我试图将数据提交到SQL Server数据库,但我收到此错误:
类型' System.Data.SqlClient.SqlException'的异常发生在System.Data.dll中但未在用户代码中处理
附加信息:必须声明标量变量" @ Name"。
但是我的变量匹配放在参数...
的变量请帮助:
protected void onclickregiPatient(object sender, EventArgs e)
{
con = new SqlConnection(conString);
con.Open();
if (FileUploadControl.HasFile)
{
string Name = txtName.Text;
string surname = txtSurname.Text;
string gender = genderRadio.Text;
string dob = txtDob.Text;
string dop = txtPob.Text;
string nationality = txtNationality.Text;
string birthcert = txtBirthCert.Text;
string nationalid = txtNationalID.Text;
string occupation = txtOccupation.Text;
byte[] photo = FileUploadControl.FileBytes;
string blood = DropDownList1.Text;
string eye = eyeColor.Text;
string skin = txtSkinColr.Text;
string height = txtHeight.Text;
string status = txtHivstatus.Text;
string phone = txtPhone.Text;
string posaddress = txtPosAddress.Text;
string physicaladdress = txtPhyAddress.Text;
string fullname = txtFullName.Text;
string relationship = txtRelationship.Text;
string phoneno = txtphoneNo.Text;
string postal = postalAddres.Text;
string physical = physicalAddress.Text;
string radio = RadioButtonList1.Text;
string type = txtType.Text;
string insertSQL = ("insert into patientProfile values(@Name, @surname, @gender, @dob, @dop, @nationality, @birthcert, @nationalid, @occupation, @photo, @blood, @eye, @skin, @height, @status, @phone, @posaddress, @physicaladdress, @fullname, @relationship, @phoneno, @postal, @physical, @radio, @type)");
cmd = new SqlCommand(insertSQL, con);
cmd.ExecuteNonQuery();
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
cmd.Parameters.Add("@surname", SqlDbType.VarChar).Value = surname;
cmd.Parameters.Add("@gender", SqlDbType.VarChar).Value = gender;
cmd.Parameters.Add("@dob", SqlDbType.VarChar).Value = dob;
cmd.Parameters.Add("@dop", SqlDbType.VarChar).Value = dop;
cmd.Parameters.Add("@nationality", SqlDbType.VarChar).Value = nationality;
cmd.Parameters.Add("@birthcert", SqlDbType.VarChar).Value = birthcert;
cmd.Parameters.Add("@nationalid", SqlDbType.VarChar).Value = nationalid;
cmd.Parameters.Add("@occupation", SqlDbType.VarChar).Value = occupation;
cmd.Parameters.Add("@photo", SqlDbType.VarBinary).Value = photo;
cmd.Parameters.Add("@blood", SqlDbType.VarChar).Value = blood;
cmd.Parameters.Add("@eye", SqlDbType.VarChar).Value = eye;
cmd.Parameters.Add("@skin", SqlDbType.VarChar).Value = skin;
cmd.Parameters.Add("@height", SqlDbType.Int).Value = height;
cmd.Parameters.Add("@status", SqlDbType.VarChar).Value = status;
cmd.Parameters.Add("@phone", SqlDbType.Int).Value = phone;
cmd.Parameters.Add("@posaddress", SqlDbType.VarChar).Value = posaddress;
cmd.Parameters.Add("@physicaladdress", SqlDbType.VarChar).Value = physicaladdress;
cmd.Parameters.Add("@relationship", SqlDbType.VarChar).Value = relationship;
cmd.Parameters.Add("@phoneno", SqlDbType.Int).Value = phoneno;
cmd.Parameters.Add("@postal", SqlDbType.VarChar).Value = postal;
cmd.Parameters.Add("@physical", SqlDbType.VarChar).Value = physical;
cmd.Parameters.Add("@radio", SqlDbType.VarChar).Value = radio;
cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
{
Response.Write("<script type=\"text/javascript\">alert('1 Row added Successfully');</script");
}
}
else
{
Response.Write("<script type=\"text/javascript\">alert('Please Select Product Image File');</script");
}
}
堆栈追踪:
[SqlException (0x80131904): Must declare the scalar variable "@Name".]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2440274
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5765240
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +285
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4169
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) +948
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +286
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +286
Botswna_Centralized_Health_Card_System.registerPatient.registerPatient.onclickregiPatient(Object sender, EventArgs e) in C:\Users\Bow-t\Documents\Visual Studio 2015\Projects\Botswna Centralized Health Card System\Botswna Centralized Health Card System\registerPatient\registerPatient.aspx.cs:140
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9767618
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1738
答案 0 :(得分:2)
为什么在添加参数之前要排除查询。
cmd = new SqlCommand(insertSQL, con);
cmd.ExecuteNonQuery();
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
首先删除cmd.ExecuteNonQuery();
答案 1 :(得分:0)
主要问题是cmd.ExecuteNonQuery();
执行了两次,所以删除第一个在本回答中注释并仅声明一次
protected void onclickregiPatient(object sender, EventArgs e)
{
con = new SqlConnection(conString);
con.Open();
if (FileUploadControl.HasFile)
{
string Name = txtName.Text;
string surname = txtSurname.Text;
string gender = genderRadio.Text;
string dob = txtDob.Text;
string dop = txtPob.Text;
string nationality = txtNationality.Text;
string birthcert = txtBirthCert.Text;
string nationalid = txtNationalID.Text;
string occupation = txtOccupation.Text;
byte[] photo = FileUploadControl.FileBytes;
string blood = DropDownList1.Text;
string eye = eyeColor.Text;
string skin = txtSkinColr.Text;
string height = txtHeight.Text;
string status = txtHivstatus.Text;
string phone = txtPhone.Text;
string posaddress = txtPosAddress.Text;
string physicaladdress = txtPhyAddress.Text;
string fullname = txtFullName.Text;
string relationship = txtRelationship.Text;
string phoneno = txtphoneNo.Text;
string postal = postalAddres.Text;
string physical = physicalAddress.Text;
string radio = RadioButtonList1.Text;
string type = txtType.Text;
string insertSQL = ("insert into patientProfile values(@Name,@surname,@gender,@dob,@dop,@nationality,@birthcert,@nationalid,@occupation,@photo,@blood,@eye,@skin,@height,@status,@phone,@posaddress,@physicaladdress,@fullname,@relationship,@phoneno,@postal,@physical,@radio,@type)");
cmd = new SqlCommand(insertSQL, con);
// cmd.ExecuteNonQuery(); comment this line
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
cmd.Parameters.Add("@surname", SqlDbType.VarChar).Value = surname;
cmd.Parameters.Add("@gender", SqlDbType.VarChar).Value = gender;
cmd.Parameters.Add("@dob", SqlDbType.VarChar).Value = dob;
cmd.Parameters.Add("@dop", SqlDbType.VarChar).Value = dop;
cmd.Parameters.Add("@nationality", SqlDbType.VarChar).Value = nationality;
cmd.Parameters.Add("@birthcert", SqlDbType.VarChar).Value = birthcert;
cmd.Parameters.Add("@nationalid", SqlDbType.VarChar).Value = nationalid;
cmd.Parameters.Add("@occupation", SqlDbType.VarChar).Value = occupation;
cmd.Parameters.Add("@photo", SqlDbType.VarBinary).Value = photo;
cmd.Parameters.Add("@blood", SqlDbType.VarChar).Value = blood;
cmd.Parameters.Add("@eye", SqlDbType.VarChar).Value = eye;
cmd.Parameters.Add("@skin", SqlDbType.VarChar).Value = skin;
cmd.Parameters.Add("@height", SqlDbType.Int).Value = height;
cmd.Parameters.Add("@status", SqlDbType.VarChar).Value = status;
cmd.Parameters.Add("@phone", SqlDbType.Int).Value = phone;
cmd.Parameters.Add("@posaddress", SqlDbType.VarChar).Value = posaddress;
cmd.Parameters.Add("@physicaladdress", SqlDbType.VarChar).Value = physicaladdress;
cmd.Parameters.Add("@relationship", SqlDbType.VarChar).Value = relationship;
cmd.Parameters.Add("@phoneno", SqlDbType.Int).Value = phoneno;
cmd.Parameters.Add("@postal", SqlDbType.VarChar).Value = postal;
cmd.Parameters.Add("@physical", SqlDbType.VarChar).Value = physical;
cmd.Parameters.Add("@radio", SqlDbType.VarChar).Value = radio;
cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
{
Response.Write("<script type=\"text/javascript\">alert('1 Row added Successfully');</script");
}
}
else
{
Response.Write("<script type=\"text/javascript\">alert('Please Select Product Image File');</script");
}
}