Hello Sir,
How to retain the selected text from dropdownlist after postback
Here based on the dropdownlist selection I'm trying to select the database but after postback the dropdownlist is setting back to it default.
Below is the code i'm using.
public partial class Login : System.Web.UI.Page
{
public static string connStr;
SqlConnection con = new SqlConnection(connStr);
protected void Page_Load(object sender, EventArgs e)
{
//Coonectionstrings for different databases
string client1 = ConfigurationManager.ConnectionStrings["client_db1"].ConnectionString;
string client2 = ConfigurationManager.ConnectionStrings["client_db2"].ConnectionString;
string client3 = ConfigurationManager.ConnectionStrings["client_db3"].ConnectionString;
if (!this.IsPostBack)
{
//Applying the select command for different databases
SqlConnection con = new SqlConnection(client1);
SqlDataAdapter da1 = new SqlDataAdapter("select provider_name,Id from Provider_details",con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
con = new SqlConnection(client2);
da1.SelectCommand.Connection = con;
DataSet ds2 = new DataSet();
da1.Fill(ds2);
con = new SqlConnection(client3);
da1.SelectCommand.Connection = con;
DataSet ds3 = new DataSet();
da1.Fill(ds3);
//Merging the database to select the provider name
ds1.Merge(ds2);
ds1.Merge(ds3);
//Binding the provider name to the dropdownlist
provider_list.DataSource = ds1;
provider_list.DataTextField = "provider_name";
provider_list.DataValueField = "Id";
provider_list.DataBind();
provider_list.Items.Insert(0, new ListItem("--Select Provider Name--", "0"));
}
}
protected void btn_login_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd1 = new SqlCommand("select * from admin where username=@username and password=@password ", con);
cmd1.Parameters.AddWithValue("@username", txt_username.Text);
cmd1.Parameters.AddWithValue("@password", txt_password.Text);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
////
if (dt1.Rows.Count > 0)
{
Response.Redirect("https://www.aspforums.net/");
}
else
{
SqlCommand cmd = new SqlCommand("select * from Userdetails where username=@username and password=@password ", con);
cmd.Parameters.AddWithValue("@username", txt_username.Text);
cmd.Parameters.AddWithValue("@password", txt_password.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
////
if (dt.Rows.Count > 0)
{
Response.Redirect("https://www.w3schools.com/");
}
else
{
Label1.Text = "Login Failed";
}
}
}
catch (Exception ex)
{
ex.ToString();
}
}
protected void provider_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (provider_list.SelectedItem.Text == "Client1")
{
connStr = (ConfigurationManager.ConnectionStrings["client_db1"].ConnectionString);
}
else if (provider_list.SelectedItem.Text == "Client2")
{
connStr = (ConfigurationManager.ConnectionStrings["client_db2"].ConnectionString);
}
else if (provider_list.SelectedItem.Text == "Client3")
{
connStr = (ConfigurationManager.ConnectionStrings["client_db3"].ConnectionString);
}
}
}
<connectionStrings>
<add name="client_db1" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Client1.mdf;Integrated Security=True"/>
<add name="client_db2" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Client2.mdf;Integrated Security=True"/>
<add name="client_db3" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Client3.mdf;Integrated Security=True"/>
</connectionStrings>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="provider_list" runat="server" AutoPostBack="true" OnSelectedIndexChanged="provider_list_SelectedIndexChanged"></asp:DropDownList>
</div><br />
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<label>Username</label><asp:TextBox ID="txt_username" runat="server"></asp:TextBox><br />
<label>Password</label><asp:TextBox ID="txt_password" runat="server"></asp:TextBox><br />
<asp:Button ID="btn_login" runat="server" Text="Login" OnClick="btn_login_Click"/>
</div>
</form>
please help