Click here to Skip to main content
15,921,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 dropdownlist and one textbox.for this i have a table named mrp which contain three fields(MRP,ITEM, RATE). the first dropdownlist i populated with MRP. whichever MRP is selected ,on the basis of that MRP's items the second dropdownlist will populate. now on the selection of the items from the second dropdownlist the rate of the items should be shown in the textbox.
i have done populating both the dropdown. can anyone tell me how i populate the textbox with rate of the selected item in the second dropdownlist. i want the answer in c#

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack) 
  { 
    SqlConnection con = new SqlConnection("Data Source=MICKEY\\SQLEXPRESS;Initial Catalog=rr;Integrated Security=True");
    SqlCommand com = new SqlCommand("select distinct MRP from mrp");
    // SqlDataReader sdr;
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(com.CommandText, con);
    con.Open();
    da.Fill(ds,"mrp");
    ddlmrp.DataSource = ds.Tables[0];
    ddlmrp.DataTextField = ds.Tables[0].Columns["MRP"].ColumnName;
    ddlmrp.DataValueField = ds.Tables[0].Columns["MRP"].ColumnName;
    ddlmrp.DataBind();
    //ddlmrp.SelectedValue = 0;

    //ddlmrp.Items.Insert(0, new ListItem("--Select--", "0"));
   }
}

protected void ddlmrp_SelectedIndexChanged(object sender, EventArgs e)
{
  SqlConnection con = new SqlConnection("Data Source=MICKEY\\SQLEXPRESS;Initial Catalog=rr;Integrated Security=True");
  SqlCommand com = new SqlCommand("select ITEM from mrp where mrp = '"+ddlmrp.SelectedValue.ToString()+"'", con);
  // SqlDataReader sdr= new SqlDataReader();
  DataSet ds = new DataSet();
  SqlDataAdapter da = new SqlDataAdapter(com);
  con.Open();
  if (ds.Tables.Contains("mrp"))
  {
    ds.Tables[0].Reset();
  }
  da.Fill(ds, "mrp");
  ddlitems.DataSource = ds.Tables[0];
  ddlitems.DataTextField = ds.Tables[0].Columns["ITEM"].ColumnName;
  ddlitems.DataValueField = ds.Tables[0].Columns["ITEM"].ColumnName;
  ddlitems.DataBind();

  //TextBox1.Text = sdr["select RATE FROM mrp where ITEM ='"+ddlitems.SelectedValue.ToString()+"'",con].ToString();

  con.Close();
}
Posted
Updated 4-Mar-16 0:09am
v2
Comments
Richard Deeming 4-Mar-16 6:00am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Member 12355460 5-Mar-16 0:42am    
how

1 solution

Use selection changed event for item dropdownlist, and get the selected item id when item dropdown list changing, use this id to pass with sql to get the rate of the corresponding item.
C#
protected void ddlitems_SelectedIndexChanged(object sender, EventArgs e)
{
  ListItem objItem = ddlitems.SelectedItem;
  string selectedItemId = objItem.Text;
  SqlConnection con = new SqlConnection("Data Source=MICKEY\\SQLEXPRESS;Initial Catalog=rr;Integrated Security=True");
  SqlCommand com = new SqlCommand("select RATE from mrp where item = '" +  selectedItemId + "'", con);
  DataTable PriceTable = new DataTable();
  SqlDataAdapter da = new SqlDataAdapter(com);
  con.Open();
  da.Fill(PriceTable);
  con.Close();

  TextBox1.Text = PriceTable.Rows[0][0].ToString().Trim();
}
 
Share this answer
 
v2
Comments
Member 12355460 5-Mar-16 0:39am    
can you elaborate it
VR Karthikeyan 5-Mar-16 1:30am    
see the solution again, i changed it.
Member 12355460 9-Mar-16 2:15am    
it actually worked. thank you so much !! :)
Member 12355460 9-Mar-16 2:10am    
thank you for your solution.

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