我是一个初学者,上上学期老师教了ASP.NET 一直没有深入理解和使用,这学期做数据库课程设计,我需要用到,捡起来时候,asp:ControlParameter 给SqlDataSource传递参数时出现的问题,希望各位高手帮我看一下,谢谢!<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!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>
<asp:TextBox ID="TextKeyWord" Name="TextKeyWord" runat="server"
ontextchanged="TextChange" Text="fra" AutoPostBack="True" DataKeyNames= "bno"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [bno],[bname] FROM teachingbuilding where([bno]=@bno)">
<SelectParameters>
<asp:ControlParameter ControlID="TextKeyWord" DefaultValue="" Name="bno" PropertyName="Text" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged" CellPadding="4"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="sno" HeaderText="sno" SortExpression="sno" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="deptno" HeaderText="deptno"
SortExpression="deptno" />
<asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
出现以下错误:
ERROR [42000] [Microsoft][SQL Native Client][SQL Server]必须声明标量变量 "@bno"。说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.Odbc.OdbcException: ERROR [42000] [Microsoft][SQL Native Client][SQL Server]必须声明标量变量 "@bno"。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。我搜索了好久都不知道原因!
看到一个解答:
如果您需要继续使用的ODBC驱动程序有,修改您的查询使用?而不是命名参数。SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = ?
这些参数用于它们添加到Parameters集合的顺序(这得到毛当您需要在查询中使用等方面的参数超过一次)。否则,你可以改变的驱动程序。寻找sybdrvodb.dll和regsvr32它。然后设置你的DSN并使用它。我没看明白!!!
希望各位前辈能帮我看下这段代码,提供解决方法,谢谢!!!
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextKeyWord" Name="TextKeyWord" runat="server"
ontextchanged="TextChange" Text="fra" AutoPostBack="True" DataKeyNames= "bno"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [bno],[bname] FROM teachingbuilding where([bno]=@bno)">
<SelectParameters>
<asp:ControlParameter ControlID="TextKeyWord" DefaultValue="" Name="bno" PropertyName="Text" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged" CellPadding="4"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="sno" HeaderText="sno" SortExpression="sno" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="deptno" HeaderText="deptno"
SortExpression="deptno" />
<asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
出现以下错误:
ERROR [42000] [Microsoft][SQL Native Client][SQL Server]必须声明标量变量 "@bno"。说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.Odbc.OdbcException: ERROR [42000] [Microsoft][SQL Native Client][SQL Server]必须声明标量变量 "@bno"。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。我搜索了好久都不知道原因!
看到一个解答:
如果您需要继续使用的ODBC驱动程序有,修改您的查询使用?而不是命名参数。SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = ?
这些参数用于它们添加到Parameters集合的顺序(这得到毛当您需要在查询中使用等方面的参数超过一次)。否则,你可以改变的驱动程序。寻找sybdrvodb.dll和regsvr32它。然后设置你的DSN并使用它。我没看明白!!!
希望各位前辈能帮我看下这段代码,提供解决方法,谢谢!!!
<SelectParameters>
<asp:ControlParameter ControlID="hf_name" Name="name" Type="string" />
</SelectParameters>
SELECT [bno],[bname] FROM teachingbuilding where [bno]=@bno 可不可以换一种绑定的方式。给你写一个。。 protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = GetDataTable("");//传你的参数进来
this.GridView1.DataBind();
} public DataTable GetDataTable(string para)//参数
{
DataTable dt = new DataTable();
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "";//你的连接字符串
SqlCommand cmd = new SqlCommand();
SqlDataReader dr = null;
cmd.CommandText = "SELECT [bno],[bname] FROM teachingbuilding where [bno]='" + para + "' ";
cmd.Connection = cn;
cn.Open();
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
if (dt.Rows.Count != 0)
{
return dt;
}
else
{
return null;
}
}
这个我没得看懂诶 是在抱歉 我初学者 我刚刚试了一下FilterExpression也可以筛选 谢谢你的回答 我去试试这段code
把这个里面的语句换下吧还有看下@bno这个参数 是从哪里传的