传递参数时语法不正确
本文关键字:语法 不正确 参数 | 更新日期: 2023-09-27 18:37:04
错误显示为"'@Cmp_DocPath'附近的语法不正确,如果我使用注释行代码,我得到的错误是"此sqlparametercollection不包含参数名称为'@Cmp_DocPath'的sql参数"。如何获取 AsyncFileUpload AJAX 控件的文件名?
protected void BtnCmpApproval_Click(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(GetConnectionString());
string query = "INSERT INTO User_Info2 VALUES (@lblCmpUserName,@txtCmpName,
@txtRegCountry,@txtCmpRegNo,@txtCmpEstdate,@txtCmpAddress,@ddlAddrIn)";
try
{
SqlCon.Open();
SqlCommand cmd = new SqlCommand(query, SqlCon);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserName", lblCmpUserName.Text);
cmd.Parameters.AddWithValue("@Cmp_Name", txtCmpName.Text);
cmd.Parameters.AddWithValue("@Commercial_RegNo", txtRegCountry.Text);
cmd.Parameters.AddWithValue("@Comm_Country", txtCmpRegNo.Text);
cmd.Parameters.AddWithValue("@Cmp_EstablishDate", txtCmpEstdate.Text);
//cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
cmd.Parameters["@Cmp_DocPath"].Value=AFU1.FileName;
cmd.Parameters.AddWithValue("@txtCmpAddress", txtCmpAddress.Text);
cmd.Parameters.AddWithValue("@ddlAddrIn", ddlAddrIn.SelectedItem.Text);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
SqlCon.Close();
}
}
您已在 Sql 插入字符串中设置了这些参数(7 个参数)
@lblCmpUserName,
@txtCmpName,
@txtRegCountry,
@txtCmpRegNo,
@txtCmpEstdate,
@txtCmpAddress,
@ddlAddrIn
您已在 SqlCommand 中添加了这些参数(8 个参数)
@UserName -> Missing
@Cmp_Name -> Missing
@Commercial_RegNo ->Missing
@Comm_Country -> Missing
@Cmp_EstablishDate ->Missing
@Cmp_DocPath ->Missing
@txtCmpAddress ->Found it !!
@ddlAddrIn ->Found it!!!!
如您所见,您错过了不止一个。我想您将控件名称与参数名称混淆了。应将插入字符串中存在的名称更改为添加到参数集合的相同名称。
string query = "INSERT INTO User_Info2 VALUES (@UserName,@Cmp_pName, " +
"@Comm_Country, @Commercial_RegNo,@Cmp_EstablishDate," +
"@txtCmpAddress,@ddlAddrIn);
您还添加了参数 Cmp_DocPath",但我在您的插入字符串中的任何地方都找不到它。
您没有添加名为 @Cmp_DocPath
的参数,您的代码只是假设它已经存在,并且它告诉您它不存在。您应该以与添加其他参数相同的方式添加参数,例如:
cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
查询参数名称与添加参数值时提供的参数名称之间应匹配。 检查所有参数名称、类型和参数计数,与您在查询中给出的内容相同。
示例代码:
try
{
string query = "INSERT INTO User_Info2 VALUES (@UserName,@Cmp_Name,@Commercial_RegNo,@Comm_Country,@Cmp_EstablishDate,@Cmp_DocPath, @txtCmpAddress,@ddlAddrIn)";
using (SqlConnection SqlCon = new SqlConnection(GetConnectionString()))
{
SqlCon.Open();
using (SqlCommand cmd = new SqlCommand(query, SqlCon))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserName", lblCmpUserName.Text);
cmd.Parameters.AddWithValue("@Cmp_Name", txtCmpName.Text);
cmd.Parameters.AddWithValue("@Commercial_RegNo", txtRegCountry.Text);
cmd.Parameters.AddWithValue("@Comm_Country", txtCmpRegNo.Text);
cmd.Parameters.AddWithValue("@Cmp_EstablishDate", txtCmpEstdate.Text);
cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
cmd.Parameters.AddWithValue("@txtCmpAddress", txtCmpAddress.Text);
cmd.Parameters.AddWithValue("@ddlAddrIn", ddlAddrIn.SelectedItem.Text);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}