简体   繁体   中英

Asp.Net GridView Datasource in aspx page

In default.aspx.cs , i defined a public variable. = my_id

And i can use this variable in my aspx page.

But i can't use in GridView DataSourceQuery.

My gridview's datasource code : ( in 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. 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:

<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 . You need to remove the ; in <%= my_id ;%> . Your query should look like

SELECT [mus_ad], [marka], [model],  [durum] FROM [servis_kayitlari]  WHERE [kullanici_id] LIKE '<%= my_id %>'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM