Ajaz
Ajaz

Reputation: 93

Error in Inserting data in SQL table

I am trying to insert data in a table (say feedback) with columns from(int) and Message(Varchar(MAX)) via c# code but it is continuously annoying me with errors. Please help, I am desperate.

Table description: From int,Message Varchar(max)

Code I'm using:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString);
con.open;
string query="insert into Feedback (From,Message) values(@frm,@msg)";
SqlCommand comm = new SqlCommand(query, con);
comm.Parameters.AddWithValue("@frm", Convert.ToInt32(TextBoxid.Text));
comm.Parameters.AddWithValue("@msg",TextBoxFeedBack.text);

comm.ExecuteNonQuery();
con.Close();

The Error I'm getting is

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'From'. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.Button1_Click(Object sender, EventArgs e) in c:\Users\Ajaz\Documents\Visual Studio 2010\WebSites\WebSite26\Default.aspx.cs:line 29

I'm guessing there's error related to data mismatch. Please help. Thanks

Upvotes: 1

Views: 1865

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98740

FROM is a reserved keyword in TSQL. You should use it with square brackets like [FROM]

string query="insert into Feedback ([From],Message) values(@frm,@msg)";

As a general recomendation, don't use reserved keywords for your identifiers and object names in your database.

Also use using statement to dispose your SqlConnection like;

string query = "insert into Feedback ([From],Message) values(@frm,@msg)";
using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString))
{
  SqlCommand comm = new SqlCommand(query, con);
  comm.Parameters.AddWithValue("@frm", Convert.ToInt32(TextBoxid.Text));
  comm.Parameters.AddWithValue("@msg",TextBoxFeedBack.text);
  con.Open();
  comm.ExecuteNonQuery();
  con.Close();
}

Upvotes: 6

Steve
Steve

Reputation: 216243

Well, the word FROM is a well known keyword in any SQL language existing in the world. If you want to use it (very bad move) then you need to encapsulate it in square brackets

string query="insert into Feedback ([From],Message) values(@frm,@msg)";

Again, don't do that, change the name of the column before having to much code to change.
Otherwise you will have this problem for the lifetime of your app.

Upvotes: 1

vmg
vmg

Reputation: 10566

"From" is the reserved word. Surround it by square brackets:

string query="insert into Feedback ([From],Message) values(@frm,@msg)";

Upvotes: 1

Related Questions