无法使用列表框更新数据库
问题描述:
foreach (ListItem LBox4Item in ListBox4.Items)
{
item = Convert.ToInt32(LBox4Item.ToString());
ListBox5.Items.Add(item.ToString());
foreach (ListItem LBox5Item in ListBox5.Items)
{
updateitem = Convert.ToInt32(LBox5Item.ToString());
string q = "update Product set BalanceOnStock='" + updateitem.ToString() + "' where ProHead='" + DDLItem.SelectedItem.Text + "'";
cm = new SqlCommand(q, cn);
cm.ExecuteNonQuery();
}
}
我正在使用带有C#Asp .net的VB Studio 2005,它实际上可以正常工作,但不能更新数据库....请帮忙或提出一些建议....
拉维·库马尔
[电子邮件地址已删除]
I am using VB Studio 2005 With C# Asp .net, It''s working practically but not update database.... please help or suggest something....
Ravi Kumar
[email address removed]
答
尝试类似的方法.我在头顶上打了这个字,因此可能需要进行一些调整.
Try something like this. I typed this off the top of my head so it may need some tweaking.
SqlConnection conn = null;
Sqlmmand cmd = new SqlCommand(conn);
string ddlItem = "";
if (DDLItem.SelectedItem != null)
{
ddlItem = (Cast to whatever type it is)DDLItem.SelectedItem.Text;
}
if (ddlItem != "")
{
try
{
conn = new SqlConnection(connectionString);
cmd = new SqlCommand(conn);
cmd.CommandType = CommandType.Text;
foreach (ListItem LBox4Item in ListBox4.Items)
{
ListBox5.Items.Add(LBox4Item);
int value = LBox4Item.ToString();
// In the code you originally posted, you're adding everything in Listbox5 everytime
// you add something to Listbox5. I'm almost positive this isn't what you wanted to to.
// So, just add the item that was recently added.
string query = string.Format("update Product set BalanceOnStock='{0}' where ProHead='{1}'", value.ToString(), ddlItem);
cmd.CommandText = query;
cmd.ExecuteNonQuery();
}
}
catch(Exception ex)
{
// determine your exception here
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}