【发布时间】:2011-03-24 10:36:26
【问题描述】:
我在 asp.net 中制作一个 WebForm,我想用来自 asp:SqlDataSource 的数据显示 gridview
当我尝试跳过(传递 null)某些参数的值时,我的问题就变成了。
这是我的代码的一些 sn-ps
在 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>
我的网格如下所示:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
麻烦在于代码背后 我的存储过程的参数之一是可选的,如果我在 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();
}
}
【问题讨论】:
标签: c# asp.net gridview sqldatasource