我是新手,也许做错了。我正在尝试使用2个不同的参数生成过滤后的视图。
当我传入一个参数时,我的代码可以正常工作
<asp:ListBox ID="ReportListItemsLb" runat="server" DataSourceID="ReportItems"
DataTextField="ReportGroupName" DataValueField="ReportGroupId" Height="134px"
Width="217px" AutoPostBack="True">
</asp:ListBox>
<asp:SqlDataSource ID="ReportItems" runat="server"
ConnectionString="<%$ ConnectionStrings:WeighBridgeDataConnectionString %>"
SelectCommand="SELECT ReportGroupName, ReportGroupId FROM MonthlyReports.ReportGroups">
</asp:SqlDataSource>
<asp:ListBox ID="DateLb" runat="server" AutoPostBack="True"
DataSourceID="MainPullBack" DataTextField="Date" DataValueField="Date"
Height="106px" Width="218px"></asp:ListBox>
<asp:SqlDataSource ID="MainPullBack" runat="server"
ConnectionString="<%$ ConnectionStrings:WeighBridgeDataConnectionString %>"
SelectCommand="SELECT distinct MonthlyReports.ReportWSDOUTData.Date
FROM MonthlyReports.ReportWSDOUT INNER JOIN MonthlyReports.ReportWSDOUTData ON
MonthlyReports.ReportWSDOUT.WSDOUTId = MonthlyReports.ReportWSDOUTData.WSDOUTId
WHERE (MonthlyReports.ReportWSDOUT.ReportGroupId = @reportgroupid) AND (MonthlyReports.ReportWSDOUT.Active = 1)">
<SelectParameters>
<asp:ControlParameter ControlID="ReportListItemsLb" Name="reportgroupid"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="AllInOneViewGV" runat="server" AutoGenerateColumns="False"
DataSourceID="AllInOneView">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Waste" HeaderText="Waste" SortExpression="Waste" />
<asp:BoundField DataField="Source" HeaderText="Source"
SortExpression="Source" />
<asp:BoundField DataField="Destination" HeaderText="Destination"
SortExpression="Destination" />
<asp:BoundField DataField="Data1" HeaderText="Data1" SortExpression="Data1" />
<asp:BoundField DataField="Data2" HeaderText="Data2" SortExpression="Data2" />
<asp:BoundField DataField="Data3" HeaderText="Data3" SortExpression="Data3" />
<asp:BoundField DataField="Data4" HeaderText="Data4" SortExpression="Data4" />
<asp:BoundField DataField="Data5" HeaderText="Data5" SortExpression="Data5" />
<asp:BoundField DataField="WSDOUTId" HeaderText="WSDOUTId"
SortExpression="WSDOUTId" />
<asp:BoundField DataField="ReportGroupId" HeaderText="ReportGroupId"
SortExpression="ReportGroupId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="AllInOneView" runat="server"
ConnectionString="<%$ ConnectionStrings:WeighBridgeDataConnectionString %>"
SelectCommand="SELECT MonthlyReports.ReportWSDOUTData.Date, MonthlyReports.ReportWSDOUT.Waste,
MonthlyReports.ReportWSDOUT.Source, MonthlyReports.ReportWSDOUT.Destination, MonthlyReports.ReportWSDOUTData.Data1,
MonthlyReports.ReportWSDOUTData.Data2, MonthlyReports.ReportWSDOUTData.Data3, MonthlyReports.ReportWSDOUTData.Data4,
MonthlyReports.ReportWSDOUTData.Data5, MonthlyReports.ReportWSDOUTData.WSDOUTId, MonthlyReports.ReportWSDOUT.ReportGroupId
FROM MonthlyReports.ReportWSDOUT INNER JOIN MonthlyReports.ReportWSDOUTData ON
MonthlyReports.ReportWSDOUT.WSDOUTId = MonthlyReports.ReportWSDOUTData.WSDOUTId
WHERE (MonthlyReports.ReportWSDOUT.ReportGroupId = @reportgroupid) AND (MonthlyReports.ReportWSDOUT.Active = 1) ">
<SelectParameters>
<asp:ControlParameter ControlID="ReportListItemsLb" Name="reportgroupid"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
但是,当我按日期添加第二个参数时,没有任何事情发生
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ListBox ID="ReportListItemsLb" runat="server" DataSourceID="ReportItems"
DataTextField="ReportGroupName" DataValueField="ReportGroupId" Height="134px"
Width="217px" AutoPostBack="True">
</asp:ListBox>
<asp:SqlDataSource ID="ReportItems" runat="server"
ConnectionString="<%$ ConnectionStrings:WeighBridgeDataConnectionString %>"
SelectCommand="SELECT ReportGroupName, ReportGroupId FROM MonthlyReports.ReportGroups">
</asp:SqlDataSource>
<asp:ListBox ID="DateLb" runat="server" AutoPostBack="True"
DataSourceID="MainPullBack" DataTextField="Date" DataValueField="Date"
Height="106px" Width="218px"></asp:ListBox>
<asp:SqlDataSource ID="MainPullBack" runat="server"
ConnectionString="<%$ ConnectionStrings:WeighBridgeDataConnectionString %>"
SelectCommand="SELECT distinct MonthlyReports.ReportWSDOUTData.Date
FROM MonthlyReports.ReportWSDOUT INNER JOIN MonthlyReports.ReportWSDOUTData ON
MonthlyReports.ReportWSDOUT.WSDOUTId = MonthlyReports.ReportWSDOUTData.WSDOUTId
WHERE (MonthlyReports.ReportWSDOUT.ReportGroupId = @reportgroupid) AND (MonthlyReports.ReportWSDOUT.Active = 1)">
<SelectParameters>
<asp:ControlParameter ControlID="ReportListItemsLb" Name="reportgroupid"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="AllInOneViewGV" runat="server" AutoGenerateColumns="False"
DataSourceID="AllInOneView">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Waste" HeaderText="Waste" SortExpression="Waste" />
<asp:BoundField DataField="Source" HeaderText="Source"
SortExpression="Source" />
<asp:BoundField DataField="Destination" HeaderText="Destination"
SortExpression="Destination" />
<asp:BoundField DataField="Data1" HeaderText="Data1" SortExpression="Data1" />
<asp:BoundField DataField="Data2" HeaderText="Data2" SortExpression="Data2" />
<asp:BoundField DataField="Data3" HeaderText="Data3" SortExpression="Data3" />
<asp:BoundField DataField="Data4" HeaderText="Data4" SortExpression="Data4" />
<asp:BoundField DataField="Data5" HeaderText="Data5" SortExpression="Data5" />
<asp:BoundField DataField="WSDOUTId" HeaderText="WSDOUTId"
SortExpression="WSDOUTId" />
<asp:BoundField DataField="ReportGroupId" HeaderText="ReportGroupId"
SortExpression="ReportGroupId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="AllInOneView" runat="server"
ConnectionString="<%$ ConnectionStrings:WeighBridgeDataConnectionString %>"
SelectCommand="SELECT MonthlyReports.ReportWSDOUTData.Date, MonthlyReports.ReportWSDOUT.Waste,
MonthlyReports.ReportWSDOUT.Source, MonthlyReports.ReportWSDOUT.Destination, MonthlyReports.ReportWSDOUTData.Data1,
MonthlyReports.ReportWSDOUTData.Data2, MonthlyReports.ReportWSDOUTData.Data3, MonthlyReports.ReportWSDOUTData.Data4,
MonthlyReports.ReportWSDOUTData.Data5, MonthlyReports.ReportWSDOUTData.WSDOUTId, MonthlyReports.ReportWSDOUT.ReportGroupId
FROM MonthlyReports.ReportWSDOUT INNER JOIN MonthlyReports.ReportWSDOUTData ON
MonthlyReports.ReportWSDOUT.WSDOUTId = MonthlyReports.ReportWSDOUTData.WSDOUTId
WHERE (MonthlyReports.ReportWSDOUT.ReportGroupId = @reportgroupid) AND (MonthlyReports.ReportWSDOUT.Active = 1) and MonthlyReports.ReportWSDOUTData.date=@date">
<SelectParameters>
<asp:ControlParameter ControlID="ReportListItemsLb" Name="reportgroupid"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DateLb" Name="date"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
任何人都可以指出我出错的地方并帮我弄清楚这样做的方法吗?
谢谢,
麦克
答案 0 :(得分:0)
只需在aspx中添加参数类型:
<asp:Parameter ControlID="ReportListItemsLb" Name="reportgroupid" PropertyName="SelectedValue" type="DateTime"></asp:Parameter>
答案 1 :(得分:0)
试试这个。
1.创建一个方法,如果在存储过程中未传递参数,则返回db null
public static object GetDataValue(object o)
{
if (o == null || String.Empty.Equals(o))
return DBNull.Value;
else
return o;
}
2.创建一个调用存储过程并填充数据集的方法。
public DataSet GetFillGvds(string param_1, string param_2) {
try
{
DataSet oDS = new DataSet();
SqlParameter[] oParam = new SqlParameter[2];
oParam[0] = new SqlParameter("@Param1", GetDataValue(param_1));
oParam[1] = new SqlParameter("@Param1", GetDataValue(param_2));
oDS = SqlHelper.ExecuteDataset(DataConnectionString, CommandType.StoredProcedure, "spTest", oParam);
return oDS;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return null;
}
}
创建数据集以绑定gridview中的数据。例如,
DataSet FillGvds = new DataSet();
param1FillGvds = "param1";
param2FillGvds = "";
FillGvds = GetFillGvds();// Assuming you have created the method to fill the dataset.
if(FillGvds != null)
{
if(FillGvds.Tables[0].Rows.Count > 0)
{
GridView1.Datasource = FillGvds;
GridView1.DataBind();
label1.Text = Convert.ToString(FillGvds.Tables[0].Rows.Count);
}
}
In order to pass the db null you query should be like this.
SELECT *(whatever you want)
FROM YourTableName
WHERE colName1 = COALESCE(@colName1, colName1) AND
colName2 = COALESCE(@colName2,colName2)