Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Found solution. It is much simpler than I thought. Instead of using <code>ddlDatabase.DataSource</code>, I used <code>ddlDatabase.Items.Add();</code> and added <code>Session["Datasource"] = null;</code>. Here are the codes:</p> <p><strong>default.aspx</strong></p> <pre><code>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 &lt; length; i++) if (Session["Error"].ToString() == errorMessage[0, i]) lblSessionInfo.Text = errorMessage[1, i]; } } </code></pre> <p><strong>db.cs</strong></p> <pre><code>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&lt;string&gt; GetDbList() { List&lt;string&gt; dataSources = new List&lt;string&gt;(); 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 &gt; 0) return true; else return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload