1

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
8
  • It's hard to see what's wrong since you're swallowing the exception. Try to add Label2.Text = ex.Message; inside catch (Exception ex) block and see if you get any error message. Commented Jun 5, 2015 at 14:15
  • there is no exception here, just no data shown in the gridview. Commented Jun 5, 2015 at 14:17
  • You don't do anything inside catch (Exception ex) block, so it looks like there's no exception and the gridview doesn't have any data. Have you tried my suggestion above? Commented Jun 5, 2015 at 14:19
  • yes, also I did a debugging of the code and it never enters in the catch-exception block Commented Jun 5, 2015 at 14:23
  • try to add value to your default value not passing empty string Commented Jun 23, 2015 at 16:07

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate your approach @FerasSalim but I already have a work around for that. Also I have updated my post with the SP, so you can see it. Still couldn't make it work using the ObjectDataSource
0

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.

1 Comment

both parameters are Int32. In both approaches I'm passing the parameters as String because ObjectDataSource2.SelectParameters["IdModel"].DefaultValue expects a String value. Did not work in the Selecting Event either. I don't know what the problem is here. Anyways I went with the last solution in my post to solve my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.