1

错误显示为“'@Cmp_DocPath' 附近的语法不正确,如果我使用注释行代码,我得到错误为“此 sqlparametercollection 不包含带有参数名'@Cmp_DocPath'的 sqlparameter”。我如何获取 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();
        }
    }    
4

3 回答 3

1

您已经在 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); 

您还添加了 Parameter Cmp_DocPath",但我在您的插入字符串中的任何地方都找不到它。

于 2012-06-14T08:56:34.547 回答
0

您没有添加带有 name 的参数@Cmp_DocPath,您的代码只是假设它已经存在,并且告诉您它不存在。您应该以与添加其他参数相同的方式添加参数,例如:

cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
于 2012-06-14T08:54:42.427 回答
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);
}
于 2012-06-14T08:55:09.103 回答