in the below quote i took from Devasya i implemented in SQL MS VS 2008, and there is an error coming any idea about it????


The code is :

db.openConnection();
             
             int j = 0;
             int k = 0;
             string[,] a = new string[100, 100];

             string data = "\"";
             for (k = 0; k < (countRow - 1); k++)
             {
                 for (j = 0; j < (countCol); j++)
                 {
                     MessageBox.Show("j=" + j);
                     MessageBox.Show("k=" + k);

                     a[j, k] = string.Format("" + dgvAList[j, k].Value);

                     if ((j == (countCol - 1)) && (k == 1))
                     {
                         data = data + a[j, k] + "'";
                     }
                     else
                         if (j < (countRow - 1))
                         {
                             data = data + a[j, k] + "','";
                         }
                     MessageBox.Show("a[" + j + "," + k + "]=" + a[j, k]);
                     MessageBox.Show("a = " + data);

                 }

                
                 String query = @"Insert Into Action (Description,AssignedTo,DueDate,Status,DateCompleted,Comment)" + " Values (" + data + ")";
                 SqlCommand command = new SqlCommand(query, DB.getConnection());
            

                 command.ExecuteNonQuery();
                 db.closeConnection();

There is an error coming in

command.ExecuteNonQuery();

saying :

Unclosed quotation mark after the character string 'True','Executivve Summary','Rohan','1/12/2010 12:00:00 AM','True','','',')'.
Incorrect syntax near 'True','Executivve Summary','Rohan','1/12/2010 12:00:00 AM','True','','',')'.

the error

why is this???

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Use Parameters.

String query = @"Insert Into Action (Description,AssignedTo,DueDate,Status,DateCompleted,Comment) values ("@p1,@p2,@p3,@p4,@p5,@p6)"

SqlCommand command = new SqlCommand(query, DB.getConnection());

command.Parameters.Add("@p1",SqlDbType.Varchar,40)
command.Parameters.Add("@p2",SqlDbType.Varchar,20)
command.Parameters.Add("@p3",SqlDbType.DateTime,8)
command.Parameters.Add("@p4",SqlDbType.Varchar,10)
command.Parameters.Add("@p5",SqlDbType.DateTime,8)
command.Parameters.Add("@p6",SqlDbType.Varchar,100)

db.openConnection();
for (k = 0; k < (countRow - 1); k++)
 {
                 for (j = 0; j < (countCol); j++)
                 {
                  command.Parameters(j).Value=dgvAList[j, k].Value;
                  }
             command.ExecuteNonQuery();
  }
db.closeConnection();
This article has been dead for over six months. Start a new discussion instead.