PDA

View Full Version : سوال: انتساب مقادیر مناسب به TextBox ها بر اساس رکورد انتخاب شده در DropDownList



meysamdalipour
شنبه 05 بهمن 1387, 23:30 عصر
من یک dropdownlist دارم و سه تا textbox . می خوام وقتی dropdownlist رو انتخاب می کنم ، بقیه فیلد ها رو تو text box ها نمایش بده .
dropdownlist = نام کالا
textbox1= کد کالا
textbox2= تعداد کالا
rextbox3= قیمت کالا

کدی که نوشتم به شرح زیر است . می خواستم بدونم کجای کارم اشتباه است ؟ آیا کسی می تونه به من کمک کنه ؟



public partial class _Default : System.Web.UI.Page
{
SqlConnection objconnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=d:\\WebSite1 \\App_Data\\supermarketing.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;");
SqlDataReader objreader;
SqlCommand objcommand;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
objcommand = new SqlCommand("SELECT codekala,namekala,tedadincarton,phisalse FROM Tbl_buy WHERE codekala=@codekala" , objconnection);

objcommand.Parameters.Add("@codekala", DropDownList1.SelectedItem.Text);
objconnection.Open();
objreader = objcommand.ExecuteReader();

TextBox1.Text = (string)objreader["namekala"];
TextBox2.Text = (string)objreader["tedadincarton"];
TextBox3.Text = (string)objreader["phisalse"];
objconnection.Close();
{

{


کسی می تونه بگه چرا این اجرا نمیشه ؟
پیامی که میده اینه :
Invalid attempt to read when no data is present.

iman_ad
یک شنبه 06 بهمن 1387, 00:38 صبح
public partial class _Default : System.Web.UI.Page
{
SqlConnection objconnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=d:\\WebSite1 \\App_Data\\supermarketing.mdf;Integrated (file://\\App_Data\\supermarketing.mdf;Integrated) Security=True;Connect Timeout=30;User Instance=True;");

SqlCommand objcommand;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
objcommand = new SqlCommand("SELECT codekala,namekala,tedadincarton,phisalse FROM Tbl_buy WHERE codekala=@codekala" , objconnection);
objcommand.Parameters.Add("@codekala", DropDownList1.SelectedItem.Text);
objconnection.Open();
SqlDataReader objreader = objcommand.ExecuteReader();
while (objreader.Read())
{
TextBox1.Text = (string)objreader[1];
TextBox2.Text = (string)objreader[2];
TextBox3.Text = (string)objreader[3];
}
}