【问题标题】:ASP.NET Error: Must declare the scalar variableASP.NET 错误:必须声明标量变量
【发布时间】:2012-02-11 21:39:16
【问题描述】:

我知道我显然缺少/错误地声明了一个变量,但我不太确定如何解决这个问题。当我单击“btnUpdate”更新数据库中的信息(基于文本字段中的内容)时,我会收到此错误。我已将以下代码发布以供审核,感谢您的帮助!

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"     Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<div class="floater">
<h3>Books</h3>
<asp:DropDownList
    id="DropDownList_Books"
    DataSourceId="srcBooks"
    DataTextField="Title"
    DataValueField="Id"
    Runat="server" />
    <asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px" 
        onclick="btnSelect_Click" />
</div>

<asp:GridView
    id="grdBooks"
    DataSourceID="srcBooks_Description"
    Runat="server" Visible="False" />

    <asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />

</div>

<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click" 
    style="z-index: 1; left: 464px; top: 153px; position: absolute; height: 26px" 
    Text="Cancel" Visible="False" />

<asp:FormView
    id="frmEditBook"
    DataKeyNames="Cat_Id"
    DataSourceId="srcBooks_Description"
    DefaultMode="Edit"
    Runat="server" Visible="False" 
    style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px">
    <EditItemTemplate>
    <asp:Label
        id="lblTitle"
        Text="Title:"
        AssociatedControlID="txtTitle"
        Runat="server" />
    <asp:TextBox
        id="txtTitle"
        Text='<%#Bind("Title")%>'
        Runat="server" />
    <br />
    <asp:Label
        id="lblDescription"
        Text="Description:"
        AssociatedControlID="txtDescription"
        Runat="server" />
    <asp:TextBox
        id="txtDescription"
        Text='<%#Bind("Description")%>'
        Runat="server" />
    <br />
    <asp:Button
        id="btnUpdate"
        Text="Update"
        CommandName="Update"
        Runat="server" />
    </EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srcBooks" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID">

</asp:SqlDataSource>

<asp:SqlDataSource ID="srcBooks_Description" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE Cat_ID=@Id" UpdateCommand="UPDATE PARTIN_INFO SET Title=@Title,
        Description=@Description WHERE Cat_Id=@Id">
<SelectParameters>
        <asp:ControlParameter
            Name="Id"
            Type="int32"                
            ControlID="DropDownList_Books" />
    </SelectParameters>

</asp:SqlDataSource>

</form>
</body>
</html>

使用新错误更新代码:在 ControlParameter 'Title' 中找不到控件 'txtTitle'。

<form id="form1" runat="server">
<div>

<div class="floater">
<h3>Books</h3>
<asp:DropDownList
    id="DropDownList_Books"
    DataSourceId="srcBooks"
    DataTextField="Title"
    DataValueField="Id"
    Runat="server" />
    <asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px" 
        onclick="btnSelect_Click" />
</div>

<asp:GridView
    id="grdBooks"
    DataSourceID="srcBooks_Description"
    Runat="server" Visible="False" />

    <asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />

</div>

<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click" 
    style="z-index: 1; left: 464px; top: 153px; position: absolute; height: 26px" 
    Text="Cancel" Visible="False" />

<asp:FormView
    id="frmEditBook"
    DataKeyNames="Cat_Id"
    DataSourceId="srcBooks_Description"
    DefaultMode="Edit"
    Runat="server" Visible="False" 

    style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px" >
    <EditItemTemplate>
    <asp:Label
        id="lblTitle"
        Text="Title:"
        AssociatedControlID="txtTitle"
        Runat="server" />
    <asp:TextBox
        id="txtTitle"
        Text='<%#Bind("Title")%>'
        Runat="server" />
    <br />
    <asp:Label
        id="lblDescription"
        Text="Description:"
        AssociatedControlID="txtDescription"
        Runat="server" />
    <asp:TextBox
        id="txtDescription"
        Text='<%#Bind("Description")%>'
        Runat="server" />
    <br />
    <asp:Button
        id="btnUpdate"
        Text="Update"
        CommandName="Update"
        Runat="server" />
    </EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srcBooks" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID">

</asp:SqlDataSource>

<asp:SqlDataSource ID="srcBooks_Description" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE Cat_ID=@Id" UpdateCommand="UPDATE PARTIN_INFO SET Title=@Title,
        Description=@Description WHERE Cat_Id=@Id">
<SelectParameters>
        <asp:ControlParameter
            Name="Id"
            Type="int32"                
            ControlID="DropDownList_Books"
            PropertyName="SelectedValue" />
    </SelectParameters>
     <UpdateParameters>
          <asp:ControlParameter Name="Title" ControlId="txtTitle" PropertyName="Text"/>
          <asp:ControlParameter Name="Description" ControlId="txtDescription" PropertyName="Text"/>
          <asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
      </UpdateParameters>

</asp:SqlDataSource>

</form>

后面还有代码:

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void srcBooks_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {
        grdBooks.Visible = true;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        frmEditBook.Visible = false;
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        frmEditBook.Visible = true;
        btnCancel.Visible = true;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            srcBooks_Description.Update();
        }
        catch (Exception except)
        {
            // Handle the Exception.
        }
    }






    }
}

【问题讨论】:

  • 请同时提供您的代码。
  • asp:ControlParameter还需要指定PropertyName="Value"
  • srcBooks_Description 也需要UpdateParameters
  • 问题在于您的 SQL 与您传递给它的参数。
  • 欢迎查看我的编辑、示例或代码修复 我是 ASP.NET 的新手

标签: asp.net scalar


【解决方案1】:

我遇到了这个问题。我把原表中的所有字段名都改成了一个单词或者多个单词之间用下划线隔开,比如card_number,修改了select和update语句,效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2017-09-24
    • 1970-01-01
    相关资源
    最近更新 更多