我正在尝试创建一个ASP.NET网页来输出文本框,然后用户将在此文本框中输入密钥,并使用该密钥在SQL代码中输入WHERE条件以输出与密钥匹配的数据。例如,用户在文本框中输入ArtistID,我需要使用
打印表SPEEDOFSALEREPORTSELECT * FROM SPEEDOFSALEREPORT WHERE ArtistID =“用户刚刚输入的密钥”
但我不知道怎么做。我在下面的代码创建一个文本框并打印表中的所有行。请给我一个提示。我整天都在谷歌搜索,但找不到答案。谢谢。
这是我更新的Default.aspx
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtArtistID" runat="server" /><br /><br />
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="submit" />
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="TRANSACTIONID" HeaderText="TRANSACTIONID"
SortExpression="TRANSACTIONID" />
<asp:BoundField DataField="WORKID" HeaderText="WORKID"
SortExpression="WORKID" />
<asp:BoundField DataField="ARTISTID" HeaderText="ARTISTID"
SortExpression="ARTISTID" />
<asp:BoundField DataField="DATEACQUIRED" HeaderText="DATEACQUIRED"
SortExpression="DATEACQUIRED" />
<asp:BoundField DataField="DATESOLDID" HeaderText="DATESOLDID"
SortExpression="DATESOLDID" />
<asp:BoundField DataField="TURNAROUNDDAYS" HeaderText="TURNAROUNDDAYS"
SortExpression="TURNAROUNDDAYS" />
<asp:BoundField DataField="ASKINGPRICE" HeaderText="ASKINGPRICE"
SortExpression="ASKINGPRICE" />
<asp:BoundField DataField="SALESPRICE" HeaderText="SALESPRICE"
SortExpression="SALESPRICE" />
<asp:BoundField DataField="SALESMARGIN" HeaderText="SALESMARGIN"
SortExpression="SALESMARGIN" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=oracle2;User ID=****;Password=****;Unicode=True"
ProviderName="System.Data.OracleClient"
SelectCommand="SELECT * FROM SPEEDOFSALEREPORT WHERE ARTISTID = @artistID;">
<SelectParameters>
<asp:ControlParameter Name="artistID" ControlID="txtArtistID" PropertyName="Text" />
</SelectParameter>
</asp:SqlDataSource>
</form>
</body>
答案 0 :(得分:2)
检查此行:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=oracle2;User ID=******;Password=***********;Unicode=True"
ProviderName="System.Data.OracleClient"
SelectCommand="SELECT * FROM SPEEDOFSALEREPORT">
正确使用:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=oracle2;User ID=******;Password=***********;Unicode=True"
ProviderName="System.Data.OracleClient"
SelectCommand="SELECT * FROM SPEEDOFSALEREPORT"
FilterExpression="Artistid='{0}'">
<FilterParameters><asp:ControlParameter Name="" ControlID="ArtistID" PropertyName="Value"/>
</FilterParameters>
答案 1 :(得分:1)
您需要使用ArtistID作为参数,还可以更改文本框并将按钮提交到ASP控件,如下所示:
<asp:TextBox ID="txtArtistID" runat="server" /><br /><br />
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="submit" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=oracle2;User ID=******;Password=***********;Unicode=True"
ProviderName="System.Data.OracleClient"
SelectCommand="SELECT * FROM SPEEDOFSALEREPORT WHERE ARTISTID = @artistID;">
<SelectParameters>
<asp:ControlParameter Name="artistID" ControlID="txtArtistID" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
然后在代码后面的按钮上单击网格(VB.NET):
Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
GridView1.DataBind()
End Sub
或(C#):
protected void btnSearch_Click(Object sender, EventArgs e)
{
GridView1.DataBind();
}
答案 2 :(得分:0)
protected void Button1_Click(object sender, EventArgs e){
if (TextBox1.Text != ""){
con.Open();
int i = 0;
SqlCommand cmd0 = new SqlCommand("select noticeid from TBLnotice where noticeid='" + TextBox1.Text + "'", con);
i= int.Parse(cmd0.ExecuteScalar().ToString());
if (i > 0){
SqlCommand cmd1 = new SqlCommand("select notice from TBLnotice where noticeid='" + TextBox1.Text + "'", con);
string name = cmd1.ExecuteScalar().ToString();
txtadds.Text = name;
TextBox1.Text = "";
txtadds.Text = "";
con.Close();
}
else{
Label7.Visible = true;
con.Close();
}
}
}