Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Below is my code for entering data table value to database
totally i have 4 fields in this DoNumber value i am taking from the variable doId1

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


            }
        }


this is the value what i am getting in Stringbuilder
{INSERT INTO Items(DoNumber,ItemNo,Description,Qty)VALUES (2011002-DO,'2','vcjj''2');}


My error msg is

The name "DO" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.>

Pls help me to resolve this

Thanks
Posted

Hi,

Try this:

C#
const string insertsql = "INSERT INTO Items(DoNumber,ItemNo,Description,Qty) VALUES ('";
            sb.AppendFormat("{0}", insertsql) + "'";


Please don't forget to vote if help...

Regards,
 
Share this answer
 
Two things I've noticed. Why don't you have a comma between the 3rd and 4th values here:
sb.AppendFormat("{0},'{1}','{2}''{3}');", doId1, splitItems[0], splitItems[1], splitItems[2]);

And also why don't you have ' around {0} since the value is not a number?

Both of these are probably causing issues, but I think the second issue is the cause of the error message. You probably need to fix both though.
 
Share this answer
 
C#
sb.AppendFormat("{0},'{1}','{2}''{3}');", doId1, splitItems[0], splitItems[1], splitItems[2])

change into
C#
sb.AppendFormat("'{0}','{1}','{2}','{3}');", doId1, splitItems[0], splitItems[1], splitItems[2])
 
Share this answer
 
1- the Value inside stringBuilder you placed has comma missing between 'vcjj' & '2'
2- what is the typeof DoNumber filed in table, is it int, if yest then you can't add do to it and if its type is varchar then the value 2011002-Do is also not surrounded with single quotation marks?
 
Share this answer
 

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