I am trying to insert a data using oledbCommand in my database in asp.net.I am taking the input through textboxes and inserting them using the insert statement.The insert code is writtern on button click event.But when i click on the button its given me error that "syntax error in insert into statement"
protected void btn_post_Click(object sender, EventArgs e)
{
con.Open();
cmd= new OleDbCommand("insert into tbl_blogs(User,Book_name,Blogs) values ('" + txt_user.Text + "' ,'" + txt_bnm.Text + "' ,'" + txt_review.Text + "') ",con);
cmd.ExecuteNonQuery();
con.Close();
}
All the fields are short text and the names are also correct.
Here is the access database:-
This is the tbl_blogs table in which i am inserting.The no field is autonumber.
No User Book_name Blogs
1 Khadija Harry potter This is one the best books that I have ever read.
THIS IS THE ERROR MESSAGE :
Syntax error in INSERT INTO statement.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
Source Error:
Line 26: cmd= new OleDbCommand("insert into tbl_blogs(User,Book_name,Blogs) values ('" + txt_user.Text + "' ,'" + txt_bnm.Text + "' ,'" + txt_review.Text + "') ",con);
Line 27: // cmd.Connection = con;
Line 28: cmd.ExecuteNonQuery();
Line 29:
Line 30: con.Close();
Source File: C:\Users\HP\Desktop\khadija visual\practicewebsite\practicewebsite\blogwrite.aspx.cs Line: 28
Stack Trace:
[OleDbException (0x80040e14): Syntax error in INSERT INTO statement.]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +1216113
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +256
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +216
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +60
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +164
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +112
practicewebsite.blogwrite.btn_post_Click(Object sender, EventArgs e) in C:\Users\HP\Desktop\khadija visual\practicewebsite\practicewebsite\blogwrite.aspx.cs:28
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9796242
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +211
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696
What I have tried:
I tried using commandtext but then got the error that object instance set to null.Cant seem to find what is causing the problem.
The value that i am trying to insert are for user = sam,book_name = Da vinci code,blog ="Best book by far".