none
C# : Get Column name for an Insert Query from The Selected GridView Row. RRS feed

  • Question

  • Hi Guys, I am trying to insert data in an SQL table by executing an Insert Query. 

    The problem is that I need to get the column name from a cell in the selected gridview Row.

    Here's the snippet:

    SqlCommand cmd30 = new SqlCommand("insert into mytable ( '"+GridView7.SelectedRow.Cells[0].Text+"' , r_id) values ('"+ GridView7.SelectedRow.Cells[1].Text +"', '"+TextBox1.Text+"')",con30);
            cmd30.ExecuteNonQuery();

    **r_id is not a Primary Key.

    The Error is get is:

    Incorrect syntax near 'r_id'.

    Any Help will be appreciated.

    Thanks,

    Shaleen


    TheHexLord

    • Moved by Kristin Xie Friday, September 5, 2014 6:41 AM
    Thursday, September 4, 2014 11:28 AM

All replies

  • Hello,

    One of the best ways to prevent this type of error is using parameters (see example). Not sure about the primary key but 99.99% of the time you insert a new record then get back the new primary key for that record. This article (done in both C# and VB.NET) with source code shows how to insert a record and get the primary key back.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, September 4, 2014 11:54 AM
  • You should remove the single quotes around the first column name:

    SqlCommand cmd30 = new SqlCommand("insert into mytable ( "+GridView7.SelectedRow.Cells[0].Text+", r_id) values ('"+ GridView7.SelectedRow.Cells[1].Text +"', '"+TextBox1.Text+"')",con30);

    And you should listen to Kevin's advice ;)

    Thursday, September 4, 2014 11:58 AM
  • hi, that didnt work. Any other suggestions?

    TheHexLord

    Thursday, September 4, 2014 1:15 PM
  • hi, that didnt work. Any other suggestions?

    TheHexLord

    Did you try my suggestion?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, September 4, 2014 1:22 PM
  • can you tell how to write a Parameterized query for this?

    Also is it possible to get the column name of the table from a specific cell of the selected Gridview row?

    That seems to be the problem.


    TheHexLord

    Friday, September 5, 2014 5:31 AM
  • Hi TheShaleen,

    Like your title mentioned.GridView control is a web class.This forum is to discuss problems of C# development.  Your question is not related to the topic of this forum.

    You'll need to post it in the dedicated ASP.Net Forum  for more efficient responses, where you can contact ASP.NET experts. Thanks for understanding.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 5, 2014 6:41 AM