I have this method in an asp page, there are no errors when executed but even no updates. What am I doing wrong in this method that does not update the table?
protected void saveSettings()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sendyourmessageConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
string userName = "something";
try
{
cmd = new SqlCommand("UPDATE Tb_Registration SET Country = @Country, City = @City Where Username = @Username" , con);
cmd.Parameters.AddWithValue("@Username", SqlDbType.VarChar);
cmd.Parameters["@Username"].Value=userName;
cmd.Parameters.AddWithValue("@Country", SqlDbType.VarChar);
cmd.Parameters["@Country"].Value= txtCountry.Text.Trim();
cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar);
cmd.Parameters["@City"].Value= txtCity.Text.Trim();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
return;
}
finally
{
con.Close();
cmd.Dispose();
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Your settings have been saved');", true);
}
}
I think the problem is AddWithValue
method.
It takes the parameter value as a second parameter, not SqlDbType
.
But don't use this method anymore. It might generate unexpected results. Use .Add()
method and it's overloads.
Read: Can we stop using AddWithValue()
already?
Also use using
statement to dispose your SqlConnection
and SqlCommand
.
using(SqlConnection con = new SqlConnection(connString))
using(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = @"UPDATE Tb_Registration SET Country = @Country, City = @City
Where Username = @Username";
cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = userName;
cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim();
cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
}
Your problem here: cmd.Parameters.AddWithValue("@Country", SqlDbType.VarChar);
It should be value of parameter, not type if you're using AddWithValue
But better practice - not to use AddWithValue
at all, as it can lead to different problems.
Instead use something like:
cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = userName;
Insert direct values rather that providing datatype
protected void saveSettings()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sendyourmessageConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
string userName = "something";
try
{
cmd = new SqlCommand("UPDATE Tb_Registration SET Country = @Country, City = @City Where Username = @Username" , con);
cmd.Parameters.AddWithValue("@Username", userName);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
cmd.Parameters.AddWithValue("@City", txtCity.Text.Trim());
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
return;
}
finally
{
con.Close();
cmd.Dispose();
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Your settings have been saved');", true);
}
}
AddWithValue的第二个参数是您的值而不是sql类型
cmd.Parameters.AddWithValue("@Username", userName);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.