ado*_*lot 4 c# asp.net gridview sqldatasource
我正在asp.net中创建一个WebForm,我想用asp中的数据显示gridview:SqlDataSource
当我尝试跳过(传递null)某些参数的值时,我的问题变成了.
这是我的代码的一些片段
在aspx文件中sqldatasource看起来像
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="art_bat_art" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="art_naz" DefaultValue="HLA" ConvertEmptyStringToNull="true" Type="String" />
<asp:Parameter Name="art_sifra" DbType="String" Direction="Input"/>
<asp:Parameter Name="vrsta_id" DefaultValue="3" ConvertEmptyStringToNull="true" Type="Int32" />
<asp:Parameter Name="podvrsta_id" DefaultValue="13" ConvertEmptyStringToNull="true"
Type="Int32" />
<asp:Parameter Name="podgrupa2" DefaultValue="1365" ConvertEmptyStringToNull="true" Type="Int32" />
<asp:Parameter Name="podosobine" DefaultValue="1:9241" ConvertEmptyStringToNull="true"
Type="String" />
<asp:Parameter Name="podosobineOR" DefaultValue="true" ConvertEmptyStringToNull="true"
Type="Boolean" />
</SelectParameters>
Run Code Online (Sandbox Code Playgroud)
我的网格看起来像这样:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
Run Code Online (Sandbox Code Playgroud)
代码后面出现问题我的存储过程的一个参数是可选的,如果我在SQL中将它作为null传递,那么无论如何都会执行存储过程.
我试图跳过并传递该参数如下
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = DBNull.Value; //Cannot implicitly convert type
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = System.Data.SqlTypes.SqlString.Null; //Cannot implicitly convert type
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = System.Data.SqlTypes.SqlString.Null.ToString(); //IT compiles but i get NULL as text in SQL
//IF I Just skip seting of this parametar, My SqlDataSource won't call stored proce
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = string.Empty; //Again no execution of stored proce
//Even if I call SqlDataSource1.DataBind(); //nothing cames to sql
GridView1.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud)
您必须编辑SQL(或存储过程)才能处理可选参数.
即:如果你有:
SELECT blah FROM yourTable WHERE art_sifra = @art_sifra
将其更改为:
SELECT blah FROM yourTable WHERE (art_sifra = @art_sifra OR @art_sifra = '')
然后只需更改ConvertEmptyStringToNull="false"并将select参数设置为"":
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = "";
还需要设置CancelSelectOnNullParameter="false"数据源.
| 归档时间: |
|
| 查看次数: |
4466 次 |
| 最近记录: |