无法使用列表框更新数据库 [英] Unable to update database using Listbox

查看:58
本文介绍了无法使用列表框更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


这篇关于无法使用列表框更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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