错误显示为“@Cmp_DocPath附近的语法不正确”,如果我使用注释行代码我得到错误为“此sqlparametercollection不包含带参数名称的'sql参数'@Cmp_DocPath'。”如何获取文件名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();
}
}
答案 0 :(得分:1)
您已在Sql Insert String中设置了这些参数(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“,但我无法在您的插入字符串中的任何位置找到它。
答案 1 :(得分:0)
您没有添加名称为@Cmp_DocPath
的参数,您的代码只是假设它已经存在,而且它告诉您它不是。您应该以添加其他参数的方式添加参数,例如:
cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
答案 2 :(得分:0)
添加参数值时,查询参数名称和参数名称之间应该匹配。检查所有参数名称,类型和参数计数,与查询中提供的参数相同。
示例代码:
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);
}