0

hi i want to insert a textbox value into one column of a table.
bt wht i m getting error as:
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '{'.

the code is

SqlCommand comm = new SqlCommand("insert into Sample {'email_id'} values("+ TextBox1.Text+")", conn);

how should i do it if i dont want to use parameter insert query.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by vuyiswamb
0
SqlCommand comm = new SqlCommand("insert into tablename (email) values (@email)", conn);
        comm.Parameters.AddWithValue("@email", TextBox1.Text);

        comm.ExecuteNonQuery();
0

That is not correct , you need to create a parameter. and assign the value of the textbox to that parameter. First you need create a StoredProcedure that will do the inserts like this

Create proc sp_Add_Record
(
@email_id int 
)
AS

INSERT INTO Sample 
VALUES(@email_id)

and in your C# Code you can do this

SqlCommand comm = new SqlCommand();
comm .CommandType = CommandType.StoredProcedure;
comm.CommandText = "sp_Add_Record"
comm.Parameters.Add("@email_id", SqlDbType.Int,4).Value = TextBox1.Text;

Now as you can see i have passed the textbox value to the parameter and it will be used in the stored procedure to do the insert

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.