Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string addquery;
foreach (DataRow dr in dt.Rows)
{
  fieldname = dr["FIELDNAME"].ToString().Trim();
  addquery = addquery + ","\"" + fieldname+"\";
  messageBox.Show(addquery);
}


OUTPUT:
"Book_id","Book_name","Book_author"


the messagebox is giving me the required output..but when i pass this variable into the insert query it through an exception and sqlcommandtext changes like that:

"\Book_id\","\Book_name\","\Book_author\"
....................................................................................

All i need is to create insert query dynamically with the required above Output..
but sql puts backward slash into string due to which i got exception..

Regards,
Hassan Tariq
Posted
Comments
Dr.Walt Fair, PE 20-Aug-11 21:26pm    
Since the problem is in creating the INSERT query, please show the code for creating the INSERT query.

Try using single quotes instead of double quotes. This is the normal text decorator for TSQL and all other dialects AFAIK
 
Share this answer
 
You shoud be using Single Quotes, incase if you are doing this for TSQL.

If you want to make your Double Quotes logic working then, do as below.

1) Use @ before your string which has Double Quote in it.
2) Use StringBuilder which will give you better performance as compare to regular string operations.
3) Make sure, you want to show messageBox inside "foreach" loop or outside.


C#
System.Text.StringBuilder addquery = new System.Text.StringBuilder();
string fieldname = string.Empty;

        foreach (DataRow dr in dt.Rows)
        {
          fieldname = dr["FIELDNAME"].ToString().Trim();

          addquery.Append(@"""");
          addquery.Append(fieldname);
          addquery.Append(@""",");
          messageBox.Show(addquery);
        }
 
Share this answer
 
Comments
Member 7995415 21-Aug-11 11:16am    
dear I am already getting the right output in messegeBox as i do code but my problem is that when i pass addquery variable in insert query it will through an exception tha is mentioned above same is the problem with the code you have provided..
Member 7995415 21-Aug-11 11:20am    
MessageBox shows "Book_id","Book_name","book_author"
but when i pass adquery in INSERT QUERY then
SQLCOMMAND changes like this "\Book_id\","\Book_name\","\Book_author\"
this is the actual problemn i had
It might help you,

Step 1) To display on the MessageBox you might try below, instead " here I used '

C#
System.Text.StringBuilder addquery = new System.Text.StringBuilder();
string fieldname = string.Empty;
foreach (DataRow dr in dt.Rows)
{
    fieldname = dr["FIELDNAME"].ToString().Trim();
    addquery.Append(@"'");
    addquery.Append(fieldname);
    addquery.Append(@"',");
    MessageBox.Show(addquery.ToString().Replace("'", "\""));
}


Step 2) For Insert statement you could try below for example,

SqlCommand command = new SqlCommand();
command.CommandText = addquery.ToString();


:)
 
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