Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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();
    }
}

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]
Posted
Updated 8-Mar-12 4:36am
v3
Comments
ZurdoDev 8-Mar-12 10:12am    
What is the error? Make sure there is a space between WHERE
Shahin Khorshidnia 8-Mar-12 10:36am    
What language is your data? Maybe you need "N" before DDLItem.Text and before updateitem.toString():


"update Product set BalanceOnStock= N'" + updateitem.ToString() + "' where ProHead=N'" + DDLItem.SelectedItem.Text + "'";
Dean Oliver 8-Mar-12 10:43am    
Whats the exact exception?

1 solution

Try something like this. I typed this off the top of my head so it may need some tweaking.
C#
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();
        }
    } 
}
 
Share this answer
 
v2

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