Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I created a small Asp.net form and trying to submit data entered in the form to the sql server database.But I am getting the following error.Please someone help.
Server Error in '/' Application.
Invalid column name 'harika'.
The multi-part identifier "harika@gmail.com" could not be bound.
Invalid column name 'India'.

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.SqlClient.SqlException: Invalid column name 'harika'.
The multi-part identifier "harika@gmail.com" could not be bound.
Invalid column name 'India'.

Source Error:
Line 48:             SqlCommand command = new SqlCommand("INSERT INTO dbo.Form(Name,Email,Mobile,Country) VALUES (" + txtname.Text + "," + txtmail.Text + ", " + txtmobile.Text + "," + ddlcountry.SelectedItem + ") ",connection);
Line 49:             
Line 50:             command.ExecuteNonQuery();
Line 51:             connection.Close();
Line 52:             

Source File: C:\Users\neeharika.ravi\documents\visual studio 2010\Projects\practise\practise\form.aspx.cs    Line: 50 

Stack Trace:
[SqlException (0x80131904): Invalid column name 'harika'.
The multi-part identifier "harika@gmail.com" could not be bound.
Invalid column name 'India'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +228
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +326
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
   practise.form.btnsubmit_Click(Object sender, EventArgs e) in C:\Users\neeharika.ravi\documents\visual studio 2010\Projects\practise\practise\form.aspx.cs:50
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
Posted
Updated 17-Jan-11 0:32am
v2
Comments
TweakBird 17-Jan-11 6:33am    
Edited for code blocks.

Try This

SQL
SqlCommand command = new SqlCommand("INSERT INTO dbo.Form(Name,Email,Mobile,Country) VALUES ('" + txtname.Text + "','" + txtmail.Text + "', " + txtmobile.Text + ",'" + ddlcountry.SelectedItem + "') ",connection);

In above statement, My assumption is 'Mobile' column is Integer.

Other wise
SQL
SqlCommand command = new SqlCommand("INSERT INTO dbo.Form(Name,Email,Mobile,Country) VALUES ('" + txtname.Text + "','" + txtmail.Text + "', '" + txtmobile.Text + "','" + ddlcountry.SelectedItem + "') ",connection);


You have to use like this. For string values '"+ value1 +"', integer values "+value2 +".

Note: Have a look on my Tip Formatting a string[^]
 
Share this answer
 
Comments
neha427 17-Jan-11 6:56am    
Thanks Eswar.
It works.
TweakBird 17-Jan-11 6:58am    
Welcome Neha427.
In Addition to answer provided by E$w@r, I would suggest you to use Parameterized query[^], Inline query you're using is prone to SQL Injection Easily.
 
Share this answer
 
Comments
TweakBird 17-Jan-11 6:55am    
Yes Hiren. I forget to mention. This is also good one & easy to practice(best way to avoid SQL Injection also).
neha427 17-Jan-11 6:57am    
Hi Hiren,
Thank you for your suggestion.
Manfred Rudolf Bihy 19-Jan-11 16:21pm    
OK, just one more! 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900