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.

Recommended Answers

All 2 Replies

SqlCommand comm = new SqlCommand("insert into tablename (email) values (@email)", conn);
        comm.Parameters.AddWithValue("@email", TextBox1.Text);

        comm.ExecuteNonQuery();

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.