[英]Asp.Net GridView Datasource in aspx page
In default.aspx.cs , i defined a public variable. 在default.aspx.cs中,我定义了一个公共变量。 = my_id
= my_id
And i can use this variable in my aspx page. 我可以在我的aspx页面中使用此变量。
But i can't use in GridView DataSourceQuery. 但是我不能在GridView DataSourceQuery中使用。
My gridview's datasource code : ( in aspx ) 我的gridview的数据源代码:(在aspx中)
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>" SelectCommand="SELECT [mus_ad], [marka], [model], [durum] FROM [servis_kayitlari] WHERE [kullanici_id] LIKE '<%= my_id ;%>'">
</asp:SqlDataSource>
After this datasource query , gridview comes null. 在此数据源查询之后,gridview变为null。 What is the problem in this query ?
此查询有什么问题?
You should use parameterized query(see Using Parameters with the SqlDataSource Control for reference) and assign parameter value in SqlDataSource.Selecting
event: 您应该使用参数化查询(请参阅对SqlDataSource控件使用参数以供参考),并在
SqlDataSource.Selecting
事件中分配参数值:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>"
SelectCommand="SELECT [mus_ad], [marka], [model], [durum] FROM [servis_kayitlari] WHERE [kullanici_id] LIKE @my_id"
OnSelecting="SqlDataSourceSelectingEventHandler">
<SelectParameters>
<asp:Parameter Name="my_id" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
And event handler: 和事件处理程序:
public void SqlDataSourceSelectingEventHandler(object sender, SqlDataSourceSelectingEventArgs e)
{
SqlDataSource1.SelectParameters["my_id"].DefaultValue = this.my_id;
}
As suggested in other answer, you should rather choose to use parametrized query but per your posted query, the issue is in binding variable with LIKE
. 正如其他答案中所建议的那样,您应该选择使用参数化查询,但是对于您发布的查询,问题出在与
LIKE
绑定变量中。 You need to remove the ;
您需要删除
;
in <%= my_id ;%>
. 在
<%= my_id ;%>
。 Your query should look like 您的查询应如下所示
SELECT [mus_ad], [marka], [model], [durum] FROM [servis_kayitlari] WHERE [kullanici_id] LIKE '<%= my_id %>'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.