Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near the keyword 'Table'.

here is the Registration Button Code.
C#
public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = "select count(*) from Table where Username='" + tbUsername.Text + "'";
            SqlCommand com = new SqlCommand(checkuser,conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            if(temp == 1)
            {
                Response.Write("User Already Exists");
            }

            conn.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            string insertQuery = "insert into Table (Username,Email,Password,Country) values(@Uname,@Email,@Passowrd,@Country)";
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@Uname",tbUsername.Text);
            com.Parameters.AddWithValue("@Email",tbEmail.Text);
            com.Parameters.AddWithValue("@Password",tbPassword.Text);
            com.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.ToString());

            com.ExecuteNonQuery();
            Response.Write("Hooray!! Registration Succesful");
        
            conn.Close();
        }
        catch(Exception ex)
        {
            Response.Write("Oops, Something Went Wrong!!" + ex.ToString());
            
        }
    }
}

Please help!!
I,m a bigner
Posted
Updated 15-Oct-15 20:45pm
v2
Comments
VR Karthikeyan 16-Oct-15 0:37am    
What is you table name? Provide your actual table name instead of giving Table in queries.
Member 12030955 16-Oct-15 1:12am    
In database, Table name is Table only. .

https://drive.google.com/file/d/0B1ZG0TY7ARVmMnNTRURBZGZqaGc/view?usp=sharing
[no name] 16-Oct-15 1:01am    
Firstly use TableName instead of "Table" because it is a reserved keyword in SQL Server.
Secondly in which method it is throwing error. Because you have provided two methods one for insert another one for select.
Thirdly use parameterized query in select statement as you are already using it insert statement.
Member 12030955 16-Oct-15 1:12am    
I,m Not getting. . . Can u plz correct the code
Member 12030955 16-Oct-15 1:09am    
It is showing exception unhandled w.r.t this statement
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

1 solution

Try this:

C#
string checkuser = "select count(*) from [Table] where Username='" + tbUsername.Text + "'";
//Here "Table"  is a predefined word in SQL Sever. You should use it like [Table].
//I hope [Table] is your table name

[EDIT]
Also modify in Button1_Click Event:
C#
string insertQuery = "insert into [Table] (Username,Email,Password,Country) values(@Uname,@Email,@Passowrd,@Country)";


And also I think you should go though: How I Write SQL, Part 1: Naming Conventions[^]

--Amy
 
Share this answer
 
v2
Comments
Member 12030955 16-Oct-15 22:41pm    
Getting this kind of message after hitting submit. I have tried everything u said.

Oops, Something went wrongSystem.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type. at System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed) at System.Data.SqlClient.SqlParameter.GetMetaTypeOnly() at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters, Boolean includeReturnValue) at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, 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 asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Registration.btnRegister_Click(Object sender, EventArgs e) in d:\Projects\myreg\Registration.aspx.cs:line 45
Member 12030955 16-Oct-15 22:43pm    
here's my button code. its executenonquery is line of error

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from [UserData] where UserName='" + txtUname.Text + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = (Int32)com.ExecuteScalar();

if (temp == 1)
{
Response.Write("User Already Exists");
}

conn.Close();
}
}
protected void btnRegister_Click(object sender, EventArgs e)
{
try
{


SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();

string insertQuery = "insert into [UserData] (UserName,Email,Password,Country) values(@Uname,@Email,@Password,@Country)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Uname", txtUname.Text);
com.Parameters.AddWithValue("@Email", txtEmail);
com.Parameters.AddWithValue("@Password", txtxPassword);
com.Parameters.AddWithValue("@Country", DropDownListCountry.SelectedItem.ToString());
com.ExecuteNonQuery();
Response.Write("yo");

conn.Close();
}
catch (Exception ex)
{
Response.Write("Oops, Something went wrong" + ex.ToString());
}
}
}

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