【发布时间】:2016-05-10 19:35:07
【问题描述】:
问题:
“System.Data.SqlClient.SqlException”类型的异常发生在 System.Data.dll 但未在用户代码中处理
附加信息:
参数化查询(@name nvarchar(7), @height float, @heightscale nvarchar(5), @weigh需要参数@monthDis,但未提供该参数
我的代码:
public partial class Step3 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection c;
string str = "Data Source =(LocalDB)\\MSSQLLocalDB;";
str += "AttachDbFilename=|DataDirectory|\\DinoData.mdf;";
str += "Integrated Security= True";
c = new SqlConnection(str);
if (Page.IsValid == true)
{
Session["yourlocation"] = ddlcountry.SelectedItem.Text;
Session["dayborn"] = DDLborn.SelectedItem.Text;
Session["monthborn"] = ddlmonthborn.SelectedItem.Text;
Session["yearborn"] = txtyar.Text;
Session["yourEmail"] = txtemail.Text;
Session["Gender"] = rbgendere.SelectedItem.Text;
Session["YourName"] = txtName.Text;
Session["YourLastName"] = txtLName.Text;
SqlCommand NewUser = new SqlCommand("INSERT INTO [DinoTable] VALUES(@name, @height, @heightscale, @weight, @weightscale, @diet, @status, @locationDis, @dayDis, @monthDis, @yearDis, @yourlocation, @dayborn, @monthborn, @yearborn, @Gender, @yourEmail, @yoname, @lastname);", c);
NewUser.Connection = c;
NewUser.Parameters.AddWithValue("@name", (string) Session["Name"]);
NewUser.Parameters.AddWithValue("@height", Convert.ToDouble(Session["Height"]));
NewUser.Parameters.AddWithValue("@heightscale", (string)Session["HeightScale"]);
NewUser.Parameters.AddWithValue("@weight", Convert.ToDouble(Session["Weight"]));
NewUser.Parameters.AddWithValue("@weightscale", (string)Session["weightscale"]);
NewUser.Parameters.AddWithValue("@diet", (string)Session["diet"]);
NewUser.Parameters.AddWithValue("@status", (string)Session["status"]);
NewUser.Parameters.AddWithValue("@locationDis", (string)Session["locationDis"]);
NewUser.Parameters.AddWithValue("@dayDis", Convert.ToInt32(Session["dayDis"]));
NewUser.Parameters.AddWithValue("@monthDis", (string)(Session["monthDis"]));
NewUser.Parameters.AddWithValue("@yearDis", Convert.ToInt32(Session["yearDis"]));
NewUser.Parameters.AddWithValue("@yourlocation", (string)Session["yourlocation"]);
NewUser.Parameters.AddWithValue("@dayborn", Convert.ToInt32(Session["dayborn"]));
NewUser.Parameters.AddWithValue("@monthborn", (string)(Session["monthborn"]));
NewUser.Parameters.AddWithValue("@yearborn", Convert.ToInt32(Session["yearborn"]));
NewUser.Parameters.AddWithValue("@Gender", (string)Session["Gender"]);
NewUser.Parameters.AddWithValue("@yourEmail", (string)Session["yourEmail"]);
NewUser.Parameters.AddWithValue("@yoname", (string)Session["YourName"]);
NewUser.Parameters.AddWithValue("@lastname", (string)Session["YourLastName"]);
NewUser.Parameters.AddWithValue("@MoneyinMilions", 3);
c.Open();
NewUser.ExecuteNonQuery();
c.Close();
Response.Redirect("finish%20new.aspx", true);
}
}
}
感谢大家的帮助!
【问题讨论】:
-
您应该查看Can we stop using AddWithValue() already? 并停止使用
.AddWithValue()- 它可能会导致意想不到和令人惊讶的结果... -
在查询中找不到参数@MoneyinMilions!
-
你能显示表 [DinoTable] 的架构吗?
-
同样不要在
INSERT语句中省略列名;我们不知道[DinoTable]包含什么或它的列的顺序。 -
我不能长时间发表评论
标签: c# sql-server