2012-06-16 105 views
-3

,我送UserName到下一个页面,如下代码Page_Load必须声明标量变量“@lblCmpUserName”

lblCmpUserName.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString()); 

,然后我要存储“公司详细信息“与SQL Server 2008表中的用户名。

我得到了错误

必须声明标量变量 “@lblCmpUserName”

代码:

protected void BtnCmpApproval_Click(object sender, EventArgs e) 
    { 
     SqlConnection SqlCon = new SqlConnection(GetConnectionString()); 
     string query = "INSERT INTO Company_Info2 VALUES (@lblCmpUserName, @txtCmpName, @txtRegCountry, @txtCmpRegNo, @txtCmpEstdate,@AFU1, @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.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

而不是存储在lblCmpUserName.Text中的查询字符串,尝试存储在另一个普通变量,如i-e字符串用户名。然后在你的sqlcommand参数里面调用'username'。然后注意它是否工作 –

回答

0

我得到了解决方案,我在我的INSERT查询中犯了一个错误。

它应该是这样的。

string query="INSERT INTO Company_Info2 VALUES  (@UserName,@Cmp_Name,@Comm_Country, 
    @Commercial_RegNo,@Cmp_DocPath, 
    @Cmp_EstablishDate,@Cmp_Address,@IsAddress)"; 
1
@lblCmpUserName 

没有匹配

cmd.Parameters.AddWithValue("@UserName", lblCmpUserName.Text); 

线@lblCmpUserName.

加入这一行:

cmd.Parameters.AddWithValue("@lblCmpUserName", lblCmpUserName.Text); 

你仍然会得到错误寿。对于您在sql语句中添加的每个@Parameter,您需要有一个匹配的cmd.Parameters.AddWithValue行。

+0

我在我的SQL表Company_Info2中为参数“@lblUserName”设置了列名“UserName”VARCHAR(16)。在我的网页中,LoginID的“UserName”显示在我的Lable控制“lblCmpUserName”。所以我试图存储(标签控制)数据与其他文本框和dropdownlist数据到我的表。为什么我得到这个错误? – Hari

+1

什么错误?如果你使用这里提供的答案,那么就不会有任何错误了。除非你的代码中有其他错误,但我们不可能知道 – Thousand

2

试试这个

protected void BtnCmpApproval_Click(object sender, EventArgs e) 
    { 
    SqlConnection SqlCon = new SqlConnection(GetConnectionString()); 
    string query = "INSERT INTO Company_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("@lblCmpUserName", lblCmpUserName.Text); 
    cmd.Parameters.AddWithValue("@txtCmpName", txtCmpName.Text); 
    cmd.Parameters.AddWithValue("@txtRegCountry", txtRegCountry.Text); 
    cmd.Parameters.AddWithValue("@txtCmpRegNo", txtCmpRegNo.Text); 
    cmd.Parameters.AddWithValue("@txtCmpEstdate", 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); 
    } 
    finally 
    { 
      SqlCon.Close(); 
    } 
    } 

其实除了最后两个,没有中加入的参数存在于查询的参数相匹配。在查询&参数定义 参数添加到使用

cmd.Parameters.AddWithValue(...) 

cmd.Parameters.Add(...) 

必须相同,否则你会得到错误肯定的命令。

2

当你在上面的代码中编写一个SqlCommand时,它就像你对NET Runtime所说的那样。 “嗨,我将这个字符串query发送给SqlServer,并使用占位符来显示命令集中的参数,请将占位符替换为您将在集合中找到的参数值。”现在,NET运行时搜索您的字符串的占位符,但没有参数相应的占位符@lblCmpUserName,所以它抱怨你。 (NET停在第一个,但你必须第一个后更多)

@lblCmpUserName, -> No match found 
@txtCmpName,  -> No match found 
@txtRegCountry, -> No match found 
@txtCmpRegNo,  -> No match found 
@txtCmpEstdate, -> No match found 
@txtCmpAddress, -> Found match 
@ddlAddrIn   -> Found match 

现在你有两个选择,改变以匹配的参数名称或更改的参数的名称相匹配的占位符的占位符。这是你的选择。

另外,在解析字符串query时,.NET运行库会注意到您有7个占位符和8个参数。这将是另一个错误。您添加参数@Cmp_DocPath但该字符串没有占位符 -