locked
Get Column name for an Insert Query from The Selected GridView Row RRS feed

  • Question

  • User-589144165 posted

    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'.

    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.

    Any Help will be appreciated.

    Thanks,

    Shaleen

    Friday, September 5, 2014 5:38 AM

Answers

  • User-1716253493 posted
    Try adding brackets "[column name]"
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 5, 2014 6:28 AM

All replies

  • User2115914008 posted

    Why you are taking column name from gridview just provide in query,

     
     SqlCommand cmd30 = new SqlCommand("insert into mytable ( 'column1', 'column2') values('" + r_id + "', '" + TextBox1.Text + "')",con30);
            cmd30.ExecuteNonQuery();

    Friday, September 5, 2014 5:59 AM
  • User-1716253493 posted
    Try adding brackets "[column name]"
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 5, 2014 6:28 AM