Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:50:15+00:00 2026-05-23T06:50:15+00:00

Everyone. I have a little issue with selecting value from the dropdown list. When

  • 0

Everyone.

I have a little issue with selecting value from the dropdown list.
When user enters his/her credentials and selects the datasource name, they are all stored in session. When the user comes back to the login page later, I want him/her to see last selected datasource name.
It works for the first time, but when I try to change the datasource name, it doesnt change but the value chosen before appears. Can you help me with this issue?
Thanks!

default.aspx

public partial class _Default : System.Web.UI.Page 
{
    private dbConnection connection = new dbConnection();
    private string[,] errorMessage = 
        { {"Expired", "Unsuccessful", "Incorrect"}, 
        {"Your Logon Session Has Expired", "Invalid Username or Password", 
            "Selected Data Source is not supported within this application"} };

    protected void Page_Load(object sender, EventArgs e)
    {

        ddlDatabase.DataTextField = "Value";
        ddlDatabase.DataValueField = "Key"; 
        ddlDatabase.DataSource = dbList.GetDbList();
        ddlDatabase.DataBind();    

        Response.Buffer = true;

        if (Session["Error"] != null)
                ErrorMessage();

        if (Session["Datasource"] != null)
            ddlDatabase.SelectedValue = Session["Datasource"].ToString();
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string dataSource = ddlDatabase.SelectedValue;
        string userID = txtUserID.Text;
        string password = txtPassword.Text;

        connection = new dbConnection(dataSource, userID, password);

        if (connection.IsValid())
        {
            try
            {
                connection.GetConnection().Open();
                Session["Username"] = userID;
                Session["Password"] = password;
                Session["Datasource"] = ddlDatabase.SelectedValue;
                Response.Clear();
                Response.Redirect("main.aspx", false);
            }
            catch (Exception ex)
            {
                lblSessionInfo.Text = ex.Message;
                Session["Username"] = null;
                Session["Password"] = null;
                Session["Error"] = "Unsuccessful";
                Response.Clear();
                Response.Redirect("default.aspx");
            }
        }
        else
        {
            Session["Username"] = null;
            Session["Password"] = null;
            Session["Datasource"] = null;
            Session["Error"] = "Incorrect";
            Response.Clear();
            Response.Redirect(Request.Url.ToString(), true);
        }
    }

    protected void ErrorMessage()
    {
        int length = errorMessage.Length / 2;

        for (int i = 0; i < length; i++)
            if (Session["Error"].ToString() == errorMessage[0, i])
                lblSessionInfo.Text = errorMessage[1, i];
    }
}

main.aspx (Logout)

public partial class main : System.Web.UI.Page
{
    dbConnection connection = new dbConnection();

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        if (Session["Username"] == null)
        {
            Session["Error"] = "Expired";
            Response.Clear();
            Response.Redirect("default.aspx");
        }
        else
        {
            Session["Error"] = null;
        }

        Label1.Text = Session["Datasource"].ToString();
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        connection.GetConnection().Close();
        Session["Username"] = null;
        Session["Password"] = null;
        Response.Clear();
        Response.Redirect(Request.Url.ToString(), true); 
    }
}

dbList.cs

public class dbList
{
    public static Dictionary<int, string> GetDbList()
    {
        //List<string> dataSources = new List<string>();
        var dataSources = new Dictionary<int, string>(); 

        RegistryKey reg = (Registry.LocalMachine).OpenSubKey("Software");
        reg = reg.OpenSubKey("ODBC");
        reg = reg.OpenSubKey("ODBC.INI");
        reg = reg.OpenSubKey("ODBC Data Sources");

        if (reg != null)
        {
            string[] items = reg.GetValueNames();
            Array.Sort(items);
            int i = 0;
            foreach (string item in items)
            {
                dataSources.Add(i, item);
                i++;
            }
        }

        reg.Close();

        return dataSources;
    }
}

dbConnection.cs

public class dbConnection
{
    private string[] dataSources = { "name1", "name2", "name3" };

    public dbConnection() {}

    public dbConnection(string dataSource, string userID, string password)
    {
        DataSource = dataSource;
        UserID = userID;
        Password = password;
    }

    public string DataSource { get; set; }
    private string UserID { get; set; }
    private string Password { get; set; }

    public OracleConnection GetConnection()
    {
        OracleConnection connection = null;

        string connectionString = 
            @"Data Source="+ DataSource + 
            ";User ID=" + UserID + 
            ";Password=" + Password + 
            ";Unicode=True";

        try
        {
            connection = new OracleConnection(connectionString);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error");
        }

        return connection;
    }

    public bool IsValid()
    {
        int trueCount = 0;

        foreach (string dataSource in dataSources)
            if (DataSource == dataSource) trueCount++;

        if (trueCount > 0) return true;
        else  return false;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:50:15+00:00Added an answer on May 23, 2026 at 6:50 am

    Found solution. It is much simpler than I thought.
    Instead of using ddlDatabase.DataSource, I used ddlDatabase.Items.Add(); and added Session["Datasource"] = null;. Here are the codes:

    default.aspx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.OracleClient;
    using System.Windows.Forms;
    
    public partial class _Default : System.Web.UI.Page 
    {
        private db connection = new db();
        private string[,] errorMessage = 
            { {"Expired", "Unsuccessful", "Incorrect"}, 
            {"Your Logon Session Has Expired", "Invalid Username or Password", 
                "Selected Data Source is not supported within this application"} };
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
    
            foreach (string name in connection.GetDbList())
                ddlDatabase.Items.Add(name);
    
            if (Session["Error"] != null)
                    ErrorMessage();
    
            if (Session["Datasource"] != null)
            {
                ddlDatabase.SelectedValue = Session["Datasource"].ToString();
                Session["Datasource"] = null;
            }
    
        }
    
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string dataSource = ddlDatabase.SelectedValue.ToString();
            string userID = txtUserID.Text;
            string password = txtPassword.Text;
    
            connection = new db(dataSource, userID, password);
    
            if (connection.IsValid())
            {
                try
                {
                    connection.GetConnection().Open();
                    Session["Username"] = userID;
                    Session["Password"] = password;
                    Session["Datasource"] = ddlDatabase.SelectedValue;
                    Response.Clear();
                    Response.Redirect("main.aspx", false);
                }
                catch (Exception ex)
                {
                    lblSessionInfo.Text = ex.Message;
                    Session["Username"] = null;
                    Session["Password"] = null;
                    Session["Datasource"] = ddlDatabase.SelectedValue;
                    Session["Error"] = "Unsuccessful";
                    Response.Clear();
                    Response.Redirect("default.aspx");
                }
            }
            else
            {
                Session["Username"] = null;
                Session["Password"] = null;
                Session["Datasource"] = null;
                Session["Error"] = "Incorrect";
                Response.Clear();
                Response.Redirect(Request.Url.ToString(), true);
            }
        }
    
        protected void ErrorMessage()
        {
            int length = errorMessage.Length / 2;
    
            for (int i = 0; i < length; i++)
                if (Session["Error"].ToString() == errorMessage[0, i])
                    lblSessionInfo.Text = errorMessage[1, i];
        }
    }
    

    db.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.OracleClient;
    using System.Windows.Forms;
    using Microsoft.Win32;
    
    public class db
    {
        private string[] dataSources = { "ValidNames" };
    
        public db() {}
    
        public db(string dataSource, string userID, string password)
        {
            DataSource = dataSource;
            UserID = userID;
            Password = password;
        }
    
        private string DataSource { get; set; }
        private string UserID { get; set; }
        private string Password { get; set; }
    
        public OracleConnection GetConnection()
        {
            OracleConnection connection = null;
    
            string connectionString = 
                @"Data Source="+ DataSource + 
                ";User ID=" + UserID + 
                ";Password=" + Password + 
                ";Unicode=True";
    
            try
            {
                connection = new OracleConnection(connectionString);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
    
            return connection;
        }
    
        public List<string> GetDbList()
        {
            List<string> dataSources = new List<string>();
    
            RegistryKey reg = (Registry.LocalMachine).OpenSubKey("Software");
            reg = reg.OpenSubKey("ODBC");
            reg = reg.OpenSubKey("ODBC.INI");
            reg = reg.OpenSubKey("ODBC Data Sources");
    
            if (reg != null)
            {
                string[] items = reg.GetValueNames();
                Array.Sort(items);
                foreach (string item in items)
                    dataSources.Add(item);
            }
    
            reg.Close();
    
            return dataSources;
        }
    
        public bool IsValid()
        {
            int trueCount = 0;
    
            foreach (string dataSource in dataSources)
                if (DataSource == dataSource) trueCount++;
    
            if (trueCount > 0) return true;
            else  return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hallo everyone, i have a list of nodes ListNode and i want to draw
I'm migrating an application from ColdFusion to ASP.Net MVC and have a little problem
I have a central git repository that everyone pushes to for testing and integration,
I have a project which contains different components that everyone works on. We have
Hi everyone my problem today is I have a couple of buttons that slide
Hello everyone Masters Of Web Delevopment :) I have a piece of PHP script
I have a problem in my project with the .designer which as everyone know
As everyone knows, sometimes developers have to document stuff. Or capture some stuff for
Everyone in my office uses Macs and therefore most use Safari. We have a
everyone seems interested in building IPhone apps today. Do you have to have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.