Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Textbox is showing Quantity after submit button1 click.I want Quantity on selection of dropdownlist.code is.......
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{


con.Open();
SqlCommand cmd1 = new SqlCommand("SELECT Quantity from AddedItem where Item_Name='" + DropDownList3.SelectedItem.Text + "'", con);
SqlDataReader dr = cmd1.ExecuteReader(CommandBehavior.SingleRow);
if (dr.Read())
{
TextBox3.Text = dr.GetValue(0).ToString();
dr.Close();
}


}

protected void Button1_Click(object sender, EventArgs e)
{
string st = "";

if (IsPostBack)
{
try
{

SqlCommand cmd1 = new SqlCommand("SELECT Item_Id from AddedItem where Item_Name='" + DropDownList3.SelectedItem.Text + "'", con);
SqlDataReader dr = cmd1.ExecuteReader(CommandBehavior.SingleRow);
if (dr.Read())
{

st = dr[0].ToString();
dr.Close();
}
SqlCommand cmd = new SqlCommand("INSERT INTO IssuedItem VALUES('" + st + "','" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "','"+DropDownList3.SelectedItem.Text+"','"+Calendar1.SelectedDate.ToShortDateString()+"','"+TextBox2.Text+"')", con);

cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{

}
finally
{
con.Close();

}
}

}
Posted
Updated 16-Nov-11 18:32pm
v4
Comments
[no name] 16-Nov-11 0:17am    
Format code snippets
[no name] 16-Nov-11 0:19am    
What textbox? What do you mean "is not working"?
Zubair Alie 16-Nov-11 0:49am    
code is fine.
may you have any quantity stored at index 0 if datarow. you can check it in (AddWatch) that what value you are receiving in dr[0].toString();

use dr.getvalue(0).Tostring(); insted of dr[0].Tostring()
 
Share this answer
 
Comments
Zubair Alie 16-Nov-11 0:54am    
dr.GetValue[0] => Java Property
dr[0].toString() => .Net Property (C#)
ujjwal uniyal 16-Nov-11 1:16am    
had it not been the property it wouldn't have been provided in the intelisence . use it, it works just the way u want it to work.
Let's see:

  • There is no such event as DropDownList3_SelectedIndexChanged. Nothing tells me that this method was actually added as an event handler to the event DropDownList3.SelectedIndexChanged. Check it up.
  • Using names like DropDownList3, SelectedIndexChangedDropDownList3_SelectedIndexChanged is invitation to trouble and violation of good Microsoft naming conventions. Yes, they were generated by Microsoft Designer, but who told you that you're supposed to keep them? Give all members some semantic names, always.
  • Don't open and close connection every time. Best of all, open it on demand using lazy evaluation pattern, http://en.wikipedia.org/wiki/Lazy_evaluation[^].
  • Why do you assume that a single row should be returned by SqlDataReader. Also, you don't have any provision to empty result of the query; in this case dr[0] may throw index-out-of-range exception.
  • Don't use string concatenation, at least not repeatedly. This is because string type is immutable. Do I have to explain why? In your case, use string.Format instead.
  • Run the code under debugger to see what's going on.


—SA
 
Share this answer
 
v2
Comments
Zubair Alie 16-Nov-11 0:40am    
Hi SAKryukov,
You are pointing out right things but the way you are pointing out it may disappoint or pressurize member. because may he is at a student level now.
treat him in the way he asked a question

hope you understand

Thanks :)
USE THE FOLLOWING CODE.

TextBox3.Text = dr.GetValue[0];
 
Share this answer
 
Comments
Zubair Alie 16-Nov-11 0:54am    
dr.GetValue[0] => Java Property
dr[0].toString() => .Net Property (C#)

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