I am new to ASP.net and I need help.
I try to add new entity to the DB and I get the following error:
Exception thrown: 'System.ArgumentException' in System.Data.dll
Reading here I guess it is something to do with the connection string but I can figure out what it is
Here is my code
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strcon);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("INSERT INTO user_master_table "
"fullname,birthday,email,phone,address,teamname,shirtnumber,hight,weight,userID,password) "
"values("
"@fullname,@birthday,@email,@phone,@address,@teamname,@shirtnumber,@hight,@weight,@userID,@password)", con);
cmd.Parameters.AddWithValue("@fullname", TextBoxFullName.Text.Trim());
cmd.Parameters.AddWithValue("@birthday", TextBoxBirthday.Text.Trim());
cmd.Parameters.AddWithValue("@email", Email.Text.Trim());
cmd.Parameters.AddWithValue("@phone", Email.Text.Trim());
cmd.Parameters.AddWithValue("@address", TextBoxAddress.Text.Trim());
cmd.Parameters.AddWithValue("@teamname", DropDownListTeamID.SelectedItem.Value);
cmd.Parameters.AddWithValue("@shirtnumber", TextBoxShirtNumber.Text.Trim());
cmd.Parameters.AddWithValue("@hight", TextBoxHeight.Text.Trim());
cmd.Parameters.AddWithValue("@weight", TextBoxWeight.Text.Trim());
cmd.Parameters.AddWithValue("@userID", TextBoxUserID.Text.Trim());
cmd.Parameters.AddWithValue("@password", TextBoxPassword.Text.Trim());
//cmd.Parameters.AddWithValue("@account_status", "pending");
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Sign Up Successful. Go to User Login to Login');</script>");
}
catch(Exception ex)
{
Response.Write("<script>alert('" ex.Message "');</script>");
}
}
Web.config
connection string:
<connectionStrings>
<add name="con"
connectionString="Data Source=DESKTOP-6S13RTK\SQLEXPRESS;Initial Catalog=BasketBallDB;Intergrated Security=true" />
</connectionStrings>
CodePudding user response:
You have a typo in your web.config
(Intergrated Security
should be Integrated Security
):
<connectionStrings>
<add name="con"
connectionString="Data Source=DESKTOP-6S13RTK\SQLEXPRESS;Initial Catalog=BasketBallDB;Integrated Security=true" />
</connectionStrings>
And your INSERT
statement is incomplete - you're missing the leading (
for the list of columns:
SqlCommand cmd = new SqlCommand("INSERT INTO user_master_table "
// here, before the first column name - add "(" ...
"(fullname, birthday, email, phone, address, teamname, shirtnumber, height, weight, userID, password) "
"VALUES ("
"@fullname, @birthday, @email, @phone, @address, @teamname, @shirtnumber, @height, @weight, @userID, @password)", con);
And the column (and variable) should probably be height
(not hight
) ...
CodePudding user response:
It seems your code was missing the opening parenthesis (
before the column list (before the fullname
column, in your case) in the SqlCommand
.
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strcon);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("INSERT INTO user_master_table "
"(fullname,birthday,email,phone,address,teamname,shirtnumber,hight,weight,userID,password) "
"values("
"@fullname,@birthday,@email,@phone,@address,@teamname,@shirtnumber,@hight,@weight,@userID,@password)", con);
cmd.Parameters.AddWithValue("@fullname", TextBoxFullName.Text.Trim());
cmd.Parameters.AddWithValue("@birthday", TextBoxBirthday.Text.Trim());
cmd.Parameters.AddWithValue("@email", Email.Text.Trim());
cmd.Parameters.AddWithValue("@phone", Email.Text.Trim());
cmd.Parameters.AddWithValue("@address", TextBoxAddress.Text.Trim());
cmd.Parameters.AddWithValue("@teamname", DropDownListTeamID.SelectedItem.Value);
cmd.Parameters.AddWithValue("@shirtnumber", TextBoxShirtNumber.Text.Trim());
cmd.Parameters.AddWithValue("@hight", TextBoxHeight.Text.Trim());
cmd.Parameters.AddWithValue("@weight", TextBoxWeight.Text.Trim());
cmd.Parameters.AddWithValue("@userID", TextBoxUserID.Text.Trim());
cmd.Parameters.AddWithValue("@password", TextBoxPassword.Text.Trim());
//cmd.Parameters.AddWithValue("@account_status", "pending");
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Sign Up Successful. Go to User Login to Login');</script>");
}
catch(Exception ex)
{
Response.Write("<script>alert('" ex.Message "');</script>");
}
}