如何根据下拉列表的选择显示表值 [英] how to show table value according to selection of dropdownlist

查看:110
本文介绍了如何根据下拉列表的选择显示表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本框在单击提交按钮1之后显示数量.我希望选择下拉列表时的数量.代码为.......
受保护的void DropDownList3_SelectedIndexChanged(对象发送者,EventArgs e)
{


con.Open();
SqlCommand cmd1 =新的SqlCommand(从AddedItem中选择数量,其中Item_Name =""+ DropDownList3.SelectedItem.Text +"'',con);
SqlDataReader dr = cmd1.ExecuteReader(CommandBehavior.SingleRow);
如果(dr.Read())
{
TextBox3.Text = dr.GetValue(0).ToString();
dr.Close();
}


}

受保护的void Button1_Click(对象发送者,EventArgs e)
{
字符串st =";

如果(IsPostBack)
{
试试
{

SqlCommand cmd1 =新的SqlCommand(从AddedItem中选择SELECT Item_Id,其中Item_Name =""+ DropDownList3.SelectedItem.Text +"'',con);
SqlDataReader dr = cmd1.ExecuteReader(CommandBehavior.SingleRow);
如果(dr.Read())
{

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

cmd.ExecuteNonQuery();
}
捕获(SqlException ex)
{

}
终于
{
con.Close();

}
}

}

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();

}
}

}

推荐答案

使用dr.getvalue(0).Tostring();插入dr [0] .Tostring()
use dr.getvalue(0).Tostring(); insted of dr[0].Tostring()


让我们看看:

  • 没有诸如DropDownList3_SelectedIndexChanged之类的事件.什么也没有告诉我该方法实际上是作为事件处理程序添加到事件DropDownList3.SelectedIndexChanged的.
  • 使用诸如DropDownList3SelectedIndexChanged DropDownList3_SelectedIndexChanged之类的名称引起麻烦,并违反了良好的Microsoft命名约定.是的,它们是由Microsoft Designer生成的,但是谁告诉您应该保留它们呢?始终为所有成员提供一些语义名称.
  • 不要每次都打开和关闭连接.最棒的是,使用惰性评估模式( http://en.wikipedia.org/)按需打开它. wiki/Lazy_evaluation [^ ].
  • 为什么您认为SqlDataReader应该返回一行.另外,您没有任何准备清空查询结果;在这种情况下,dr[0]可能会抛出索引超出范围的异常.
  • 请勿使用字符串连接,至少不要重复使用.这是因为字符串类型是不可变.我必须解释为什么吗?您可以使用string.Format代替.
  • 在调试器下运行代码以查看发生了什么.
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.


使用以下代码.

TextBox3.Text = dr.GetValue [0];
USE THE FOLLOWING CODE.

TextBox3.Text = dr.GetValue[0];


这篇关于如何根据下拉列表的选择显示表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆