Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am using visual studio 2015, I have a problem with insert data to database of SQL server.
Here is my code:
C#
try
        {
            SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Mohammad\Desktop\loginpage\App_Data\CMS.mdf;Integrated Security=True");
            conn.Open();
            string insert_query = "insert into Table (username,email,password,country) values (@username ,@email ,@password ,@country)";
            SqlCommand cmd = new SqlCommand(insert_query, conn);
            cmd.Parameters.AddWithValue("@username", t_username.Text);
            cmd.Parameters.AddWithValue("@email", t_email.Text);
            cmd.Parameters.AddWithValue("@password", t_password.Text);
            cmd.Parameters.AddWithValue("@country", D_country.SelectedItem.ToString());

            cmd.ExecuteNonQuery();
            Response.Redirect("manegar.aspx");
            Response.Write("Registration Is Successfully");
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("ERORR:" + ex.ToString());

        }
    }
}


What I have tried:

ERORR:System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'Table'. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at login.Button1_Click(Object sender, EventArgs e) in c:\Users\Mohammad\Desktop\loginpage\login.aspx.cs:line 33 ClientConnectionId:fb623e67-d06e-45d3-9133-53f264d26604 Error Number:156,State:1,Class:15
Posted
Updated 12-Apr-21 23:23pm
v2
Comments
[no name] 7-Apr-17 12:39pm    
I have a problem reading the description of your problem.
Karthik_Mahalingam 7-Apr-17 12:41pm    
what is the error message
Richard Deeming 7-Apr-17 13:55pm    

1 solution

Table is a reserved word.

If you are going to call a table Table (hint, don't) then you must surround it with square brackets thus [Table] i.e.
C#
string insert_query = "insert into [Table] (username,email,password,country) values (@username ,@email ,@password ,@country)";
 
Share this answer
 
Comments
Karthik_Mahalingam 7-Apr-17 12:58pm    
5
MuhammadNaamh 8-Apr-17 6:34am    
Thanks its work
CHill60 8-Apr-17 7:39am    
Good. Feel free to press the green "Accept" button to close off the question.

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