Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am using dropdownlist and that has item retriveing from database, my question is that item select any one item that item value will display at text box?

What I have tried:

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

    SqlConnection con1 = new SqlConnection("Data Source=XENORIX8-PC;Initial Catalog=xenorix;User ID=******;Password=*****");
    SqlCommand cmd1 = new SqlCommand("select item_price from quantity where item_name='" + DropDownList1.SelectedItem.Text + "'", con1);
    SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
    DataTable dt1 = new DataTable();
    sda1.Fill(dt1);
    TextBox1.Text = dt1.Rows[0].ToString();
}

Is this correct or not?, If its wrong please send any clue or code
Posted
Updated 1-May-16 22:27pm
v3
Comments
VR Karthikeyan 2-May-16 3:16am    
Is it working? Or showing any errors? Tell us what is your problem. Where you stuck?
Nagarjuna99 2-May-16 3:20am    
no errors,but value was not come into textbox
Suvendu Shekhar Giri 2-May-16 3:36am    
Never share userid/passwords in community forums. Removed from question.
Suvendu Shekhar Giri 2-May-16 3:38am    
Instead of DropDownList1.SelectedItem.Text try DropDownList1.SelectedValue for filtering the respective value field.
Nagarjuna99 2-May-16 3:44am    
my requirement is if dropdownlist select one item auto,automatiaclly that value come on text box, what changes i'am taken to my requerment. once see my tried pattren please .


use below code in last line

C#
TextBox1.Text = dt1.Rows[0][0].ToString();
 
Share this answer
 
Comments
Nagarjuna99 2-May-16 3:35am    
i'am trying this both ways,but its value come on button click time,my requirement is if dropdoenlist item selected than automatically that value will be come on text box.
Try this

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
       {

           SqlConnection con1 = new SqlConnection("Your Connection string");
           SqlCommand cmd1 = new SqlCommand("select item_price from quantity where item_name=@item", con1);
           cmd1.Parameters.Add("@item", DropDownList1.SelectedItem.Text);
           SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
           DataTable dt1 = new DataTable();
           sda1.Fill(dt1);
           if (dt1.Rows.Count > 0) // validate for avoiding index exception.
               TextBox1.Text = dt1.Rows[0]["item_price"].ToString(); // define the column name
       }


You should be aware of SQL Injection[^] when hardcoding the SQl Statemetns
 
Share this answer
 
Comments
Nagarjuna99 2-May-16 4:43am    
both my code and,this code are same answer,if select the item of drop down list and press button and after that value come on text box,i need select item from drop down list than value will come on text box without pressing button key.
Karthik_Mahalingam 2-May-16 4:50am    
add AutoPostback="true" as below
<asp:DropDownList runat="server" AutoPostBack="true" ID="DropDownList1">
.
.
.
Nagarjuna99 2-May-16 5:02am    
ya thanks sir its working but, items are re adding into dropdownlist,what can i do?
Karthik_Mahalingam 2-May-16 5:08am    
post your full code, i need to see the binding
I think you are missing something
Nagarjuna99 2-May-16 5:13am    
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=XENORIX8-PC;Initial Catalog=xenorix;User ID=sa;Password=123");
SqlCommand cmd = new SqlCommand("select item_name from quantity", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Open();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "item_name";
DropDownList1.Items.Add(new ListItem("SELECT PRODUCT", "-1"));
DropDownList1.DataBind();
con.Close();


}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection("Data Source=XENORIX8-PC;Initial Catalog=xenorix;User ID=sa;Password=123");
SqlCommand cmd1 = new SqlCommand("select item_price from quantity where item_name=@item", con1);
cmd1.Parameters.Add("@item", DropDownList1.SelectedItem.Text);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
sda1.Fill(dt1);
if (dt1.Rows.Count > 0) // validate for avoiding index exception.
TextBox1.Text = dt1.Rows[0]["item_price"].ToString(); // define the column name
/*
SqlCommand cmd1 = new SqlCommand("select item_price from quantity where item_name='" + DropDownList1.SelectedValue+ "'", con1);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
sda1.Fill(dt1);

TextBox1.Text = dt1.Rows[0][0].ToString();

*/


}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}

protected void TextBox3_TextChanged(object sender, EventArgs e)
{

}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900