Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi

I have to insert grid textbox value from datatable to database.
But my code is working for single row for multi row it is not working.
Could you pls correct

const string insertsql = "INSERT INTO Items(Description,ItemNo,Qty)VALUES (";
        sb.AppendFormat("{0}", insertsql);
        foreach (string item in sc)
        {
            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());
                sb.AppendFormat("'{0}','{1}','{2}');", splitItems[0], splitItems[1], splitItems[2]);


            }
        }


In string builder i am getting the values below is example
INSERT INTO Items(Description,ItemNo,Qty)VALUES ('1','DHDHJDHJ','1');'2','BHHHH','2');"



This is my error msg Incorrect syntax near '2'.

Thanks
Posted

1 solution

Doing it this way would get you proper query.
C#
foreach (string item in sc)
{
    string insertsql = "INSERT INTO Items(Description,ItemNo,Qty)VALUES (";
    sb.AppendFormat("{0}", insertsql);
    if (item.Contains(","))
    {
        splitItems = item.Split(",".ToCharArray());
        sb.AppendFormat("'{0}','{1}','{2}');", splitItems[0], splitItems[1], splitItems[2]);


    }
}
 
Share this answer
 
Comments
Lancy.net 14-Nov-11 23:58pm    
Thanks a lot Prerak.
I have a query could you please help

My project is invoice Management
i have tables like customer details , Items & Calculation
i am confused with the Item Table since this is my first project not able to design properly

item table i have fields like Item No,Description,Qty,donumber,dodate i cannot make any fields us primary key from this(values not unique). these values i am taking from dynamic textboxes and saving into datatable then i am inserting it to database.i have two other field also like dono and doDate. I am planned Dono to make us primarykey.
But really no idea how to add donumber and dodate value to the item table.since i am inserting first 3 values from datatable to item.

could you pls advice how to do?

Thanks for your help

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