Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows;

C#
private void Btn_Save_Click(object sender, EventArgs e)
      {
          for (int i = 0; i < DGVCalendar.RowCount; i++)
          {

              if (Convert.ToBoolean(this.DGVCalendar[0, i].Value) == true)
                 sql = "insert into Tb_Faculty_Availability ([Faculty_Code],[Available_date])" + " values('" + cb_Faculty_Code.Text + "','" + Convert.ToDateTime(DGVCalendar.Rows[i].Cells[1].Value.ToString()) + "',";
              try
              {
                  GFun.Error = "";
                  GFun.InsertAccessData(sql);
                  if (GFun.Error.ToString() != "")
                  {
                      MessageBox.Show(GFun.Error.ToString(), "Error");
                      this.Cursor = Cursors.Arrow;
                      return;
                  }
                  GFun.OleDbCon.Close();
                  MessageBox.Show("Records Inserted Successfully", "Record Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }

              catch (Exception Ex)
              {
                  MessageBox.Show(Ex.ToString(), "Error");
                  this.Cursor = Cursors.Arrow;
                  return;
              }
          }
      }

in run mode as follows;

select the faculty from combobox and select the month calendar then partiuclar month selected date will display in the datagridview.

then selected date in datagridview is saved in the database.


for that i written a code

when i run click the save button error shows as follows;

Syntax error in insert into statement.

My query as follows;


C#
sql = "insert into Tb_Faculty_Availability ([Faculty_Code],[Available_date])" + " values('" + cb_Faculty_Code.Text + "','" + Convert.ToDateTime(DGVCalendar.Rows[i].Cells[1].Value.ToString()) + "'";
what is the mistake, in my above query.

please help me.

Regards,
Narasiman P.
Posted
Updated 7-Mar-13 3:03am
v2

First, get rid of all that string concatentation and replace it with a parameterized INERT query. That error you're getting is easily resolved by not doing what you're doing. By simplifying your code you're making it easier to debug and maintain.

You'll also remove the massive SQL Injection Attack problems you've exposed yourself to because of the way you nievely wrote your code.

Google for "Parameterized SQL Queries" and "SQL Injection Attack" for more information and examples.
 
Share this answer
 
Comments
[no name] 7-Mar-13 8:54am    
for that how can i use the paramterized query, for my insert query.

please help me.

regards,
Narasiman P.
Dave Kreskowiak 7-Mar-13 10:41am    
If your definition of "help" is write your code for you, you've come to the wrong place. If I did that, you wouldn't have any clue as to why I did the things I did.

I gave you everything you need to solve this yourself. All you have to do is put those search phrases into Google and teach yourself about what you should be doing and why.
Maciej Los 7-Mar-13 9:09am    
Short and to the point!
+5
As Dave Kreskoviak wrote, you need to know how to avoid injection attack. Please use Google to find listed subjects.
Next, follow the link: SQLParameterCollection.AddWithValue() method[^]
 
Share this answer
 
If you look at query that you have written in your code, you have extra comma at the end of the query. Please remove it and then try.
C#
sql = "insert into Tb_Faculty_Availability ([Faculty_Code],[Available_date])" + " values('" + cb_Faculty_Code.Text + "','" + Convert.ToDateTime(DGVCalendar.Rows[i].Cells[1].Value.ToString()) + "',";


Please remove this comma -----> "',"; from sql string.
 
Share this answer
 
v3

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