Member
7 Points
73 Posts
Jul 20, 2018 05:03 PM|Omar27|LINK
mudassarkhan her's my code ===>
HTML:
<div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" Height="17px" Width="249px"> </asp:DropDownList> <br /> <br /> Name<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <br /> Surname<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> <br /> Username<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br /> <br /> Password<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <br /> <br /> Role<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Update" /> <asp:Button ID="Button2" runat="server" Text="delete" /> </div>
Behind code :
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.Items.Add(new ListItem("--Select--", "")); DropDownList1.AppendDataBoundItems = true; String strConnString = ConfigurationManager .ConnectionStrings["parallelConnectionString"].ConnectionString; String strQuery = "select name, surname from tblusers"; MySqlConnection con = new MySqlConnection(strConnString); MySqlCommand cmd = new MySqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = strQuery; cmd.Connection = con; try { con.Open(); DropDownList1.DataSource = cmd.ExecuteReader(); DropDownList1.DataTextField = "name"; DropDownList1.DataValueField = "surname"; DropDownList1.DataBind(); } catch (Exception ex) { throw ex; } finally { con.Close(); con.Dispose(); } } }
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) { String strConnString = ConfigurationManager .ConnectionStrings["parallelConnectionString"].ConnectionString; string strQuery = "select name, surname, username, password, role from tblusers where name = @name"; MySqlConnection con = new MySqlConnection(strConnString); MySqlCommand cmd = new MySqlCommand(); cmd.Parameters.AddWithValue("@name", DropDownList1.SelectedItem.Text); cmd.CommandType = CommandType.Text; cmd.CommandText = strQuery; cmd.Connection = con;
try { con.Open(); MySqlDataReader sdr = cmd.ExecuteReader(); if (sdr.HasRows) { while (sdr.Read()) { TextBox1.Text = sdr[0].ToString(); TextBox2.Text = sdr[1].ToString(); TextBox3.Text = sdr[2].ToString(); TextBox4.Text = sdr[3].ToString(); TextBox5.Text = sdr[4].ToString(); } } else { TextBox1.Text = "not found"; } } finally { con.Close(); con.Dispose(); }
}
i added two buttons UPDATE and DELETE and hopefuly work .
Member
7 Points
73 Posts
Re: Update and delete data from dropdownlist
Jul 20, 2018 05:03 PM|Omar27|LINK
mudassarkhan her's my code ===>
HTML:
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" Height="17px" Width="249px">
</asp:DropDownList>
<br />
<br />
Name<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Surname<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
Username<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
Password<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<br />
Role<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Update" />
<asp:Button ID="Button2" runat="server" Text="delete" />
</div>
Behind code :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("--Select--", ""));
DropDownList1.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["parallelConnectionString"].ConnectionString;
String strQuery = "select name, surname from tblusers";
MySqlConnection con = new MySqlConnection(strConnString);
MySqlCommand cmd = new MySqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "surname";
DropDownList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
String strConnString = ConfigurationManager
.ConnectionStrings["parallelConnectionString"].ConnectionString;
string strQuery = "select name, surname, username, password, role from tblusers where name = @name";
MySqlConnection con = new MySqlConnection(strConnString);
MySqlCommand cmd = new MySqlCommand();
cmd.Parameters.AddWithValue("@name", DropDownList1.SelectedItem.Text);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
MySqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows) {
while (sdr.Read())
{
TextBox1.Text = sdr[0].ToString();
TextBox2.Text = sdr[1].ToString();
TextBox3.Text = sdr[2].ToString();
TextBox4.Text = sdr[3].ToString();
TextBox5.Text = sdr[4].ToString();
}
}
else
{
TextBox1.Text = "not found";
}
}
finally
{
con.Close();
con.Dispose();
}
}
i added two buttons UPDATE and DELETE and hopefuly work .