问题描述
我是新来这,也许做的事情是错误的。我试图用产生2个不同参数的过滤视图。
I'm new to this and maybe doing things wrong. I'm trying to produce a filtered view using 2 different parameters.
当我传递一个参数我的code ++工程
When I pass in one parameter my code works
<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>
但是,当我添加第二个参数按日期没有过滤发生
But when I add a second parameter to filter by date nothing happens
<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>
任何人都可以指出我要去的地方错了,帮我找出这样做的方法吗?
Can anyone point out where I am going wrong and help me figure out a way of doing this?
谢谢,
迈克
推荐答案
尝试了这一点。
1,创建将返回分贝空,如果参数没有在存储过程中传递的方法
1.Create a method which will return db null if parameter is not passed in the stored procedure
public static object GetDataValue(object o)
{
if (o == null || String.Empty.Equals(o))
return DBNull.Value;
else
return o;
}
2.创建一个将调用的存储过程并填充DataSet的方法。
2.Create a method which will called the stored procedure and fill the dataset.
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;
}
}
Create(创建)在GridView数据绑定的数据集。例如,
Create a dataset to bind data in the gridview. For instance,
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)
这篇关于如何更新有2个参数的GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!