Somebody
Somebody

Reputation: 2779

Gridview Binded to ObjectDataSource not showing data ASP.Net

I have these two gridviews using the same approach to show data. The first one returns data as expected. Now, the second one is not returning data at all, and I can't find why? Can someone please look into this to tell me what on earth is going on here?

.aspx

<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="btnAction" runat="server" Text="Action" 
        onclick="btnAction_Click" />
    <br />
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Text="Grid1 Empty"></asp:Label>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:BoundField DataField="ReleaseId" HeaderText="ReleaseId" 
                SortExpression="ReleaseId" />
            <asp:BoundField DataField="ReleaseSubmitDate" HeaderText="ReleaseSubmitDate" 
                SortExpression="ReleaseSubmitDate" />
            <asp:BoundField DataField="ExpirationDate" HeaderText="ExpirationDate" 
                SortExpression="ExpirationDate" />
            <asp:BoundField DataField="ExpirationStatus" HeaderText="ExpirationStatus" 
                ReadOnly="True" SortExpression="ExpirationStatus" />
        </Columns>
    </asp:GridView>
    <br />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" Text="Grid2 Empty"></asp:Label>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
        DataSourceID="ObjectDataSource2">
        <Columns>
            <asp:BoundField DataField="BinLoc" HeaderText="BinLoc" 
                SortExpression="BinLoc" />
            <asp:BoundField DataField="Product" HeaderText="Product" 
                SortExpression="Product" />
            <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                SortExpression="Quantity" />
            <asp:BoundField DataField="Reserved" HeaderText="Reserved" 
                SortExpression="Reserved" />
            <asp:BoundField DataField="SiteId" HeaderText="SiteId" 
                SortExpression="SiteId" />
            <asp:BoundField DataField="Zone" HeaderText="Zone" SortExpression="Zone" />
            <asp:BoundField DataField="WOAAvail" HeaderText="WOAAvail" 
                SortExpression="WOAAvail" />
            <asp:BoundField DataField="InWO" HeaderText="InWO" SortExpression="InWO" />
            <asp:BoundField DataField="QtyResrv" HeaderText="QtyResrv" 
                SortExpression="QtyResrv" />
            <asp:BoundField DataField="QtyAvail" HeaderText="QtyAvail" 
                SortExpression="QtyAvail" />
        </Columns>
    </asp:GridView>
    <br />
    <br />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
        TypeName="DALTest.DAL.dsReleaseTableAdapters.sp_ET_SWR_GetReleases_ByModelId_CustomerId_RegionIdTableAdapter">
        <SelectParameters>
            <asp:Parameter Name="ModelId" Type="Int32" />
            <asp:Parameter Name="CustomerId" Type="Int32" />
            <asp:Parameter Name="RegionId" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"             
        TypeName="DALTest.DAL.dsAvailabilityTableAdapters.sprGetRBProdBinDetlTableAdapter">    
        <SelectParameters>
            <asp:Parameter Name="InvtId" Type="String" />
            <asp:Parameter Name="IdModel" Type="Int32" />
            <asp:Parameter Name="SiteId" Type="String" />
        </SelectParameters>        
    </asp:ObjectDataSource>
</div>
</form>

aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Label1.Visible = false;
            Label2.Visible = false;
        }
    }

    protected void btnAction_Click(object sender, EventArgs e)
    {
        Label1.Visible = false;
        Label2.Visible = false;
        FillResults();
        FillResults2();
    }

    private void FillResults()
    {
        ObjectDataSource1.SelectParameters["ModelId"].DefaultValue = "7461";
        ObjectDataSource1.SelectParameters["CustomerId"].DefaultValue = "112";
        ObjectDataSource1.SelectParameters["RegionId"].DefaultValue = "91";

        try
        {
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }

        if (GridView1.Rows.Count == 0)
        {
            Label1.Visible = true;
        }
    }

    private void FillResults2()
    {
        ObjectDataSource2.SelectParameters["InvtId"].DefaultValue = "11000020982";
        ObjectDataSource2.SelectParameters["IdModel"].DefaultValue = "";
        ObjectDataSource2.SelectParameters["SiteId"].DefaultValue = "";

        try
        {
            GridView2.DataBind();
        }
        catch (Exception ex)
        {
        }

        if (GridView2.Rows.Count == 0)
        {
            Label2.Visible = true;
        }
    }        

And the stored procedure is returning data for the selected parameters:

enter image description here

Now, if I do this:

private void FillResults(string aProduct, string aIdModel, string aSiteId)
    {
        using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ETConnStr"].ToString()))
        {
            try
            {
                SqlCommand Cmd = new SqlCommand("sprGetRBProdBinDetl", Conn);
                Cmd.Parameters.Add("@InvtId", SqlDbType.VarChar, 30).Value = aProduct;
                Cmd.Parameters.Add("@IdModel", SqlDbType.Int).Value = aIdModel;
                Cmd.Parameters.Add("@SiteId", SqlDbType.VarChar, 20).Value = aSiteId;
                Cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter adapter = new SqlDataAdapter(Cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "PNAvail");
                GridView2.DataSource = ds;
                GridView2.DataBind();

            }
            catch (Exception ex)
            {
            }
        }
    }

Then the gridview shows data, so I don't know what could be wrong here :(

SP code snippet

PROCEDURE [dbo].[sprGetRBProdBinDetl] 
@InvtId varchar(30) = '', @IdModel int = 0, @SiteId varchar(20) = ''
AS

if @IdModel = 0 --> This will be always the case
begin   
    select 
        BINLABEL BinLoc, 
        Product, 
        Quantity, 
        Reserved, 
        ClientName SiteId, 
        Zone
    from 
        tblTable
    where 
        product = case when @InvtId = '' then product else @InvtId end
        and ClientName = case when @siteid = '' then ClientName else @SiteId end
end

Upvotes: 1

Views: 918

Answers (2)

Feras Salim
Feras Salim

Reputation: 438

you have to add values in your parameter default value, else the query will return nothing try this:

private void FillResults2()
    {
        ObjectDataSource2.SelectParameters["InvtId"].DefaultValue="11000020982";
        ObjectDataSource2.SelectParameters["IdModel"].DefaultValue = "1";
        ObjectDataSource2.SelectParameters["SiteId"].DefaultValue = "1";

        try
        {
            GridView2.DataBind();
        }
        catch (Exception ex)
        {
        }

        if (GridView2.Rows.Count == 0)
        {
            Label2.Visible = true;
        }

#Update1

public DataSet Getdata(string IdModel)
{
 if (IdModel!= null)
    if (IdModel.Trim() != "")
    {
        yourquery= yourquery.Where(c => c.IdModel== IdModel.Trim());
    }
//and so on
}

Upvotes: 1

codeandcloud
codeandcloud

Reputation: 55200

The only difference in the both approaches is that @IdModel is of type SqlDbType.Int in your second approach and in the first approach you are passing it as a string. Try this

Parameter parm = new Parameter("IdModel",TypeCode.Int32);
ObjectDataSource2.SelectParameters.Add(parm);

If this doesn't work you could also try the Selecting event of the ObjectDataSource.

Upvotes: 0

Related Questions