ANIL
ANIL

Reputation: 1

Null Value in Label object

I have created on GridView with Label. I have written store procedure to get StatusCode

SELECT StatusCode
From TableName

This line in GridView

< asp:Label ID="lblStatusCode" runat="server" Visible="false"

Text='<%#DataBinder.Eval(Container.DataItem, "StatusCode")%>' />

These lines in .cs file

Label lblStatusCode = (Label)row.FindControl("lblStatusCode");
objJV.Status = Convert.ToInt32(lblStatusCode.Text);

but in lblStatusCode.Text it is showing NULL even though there is value in Table.

When I execute stored procedure independently it is giving values.

// bind function

protected void Page_Load(object sender, EventArgs e) {

        if (!IsPostBack)
        {
            BindJVJobValidationDetails();

        }


    }

protected void BindJVJobValidationDetails() {

        JVSummary objJV = new JVSummary();

        DataSet dataJobValidation = new DataSet();

        if (SessionVariables.PERID != null)
        {

            dataJobValidation = objJV.GetjvTransaction(SessionVariables.PERID);

            gvEmployee.DataSource = dataJobValidation;
            gvEmployee.DataBind();

        }

    }

What might be the problem...?

Upvotes: 0

Views: 2291

Answers (2)

Brijesh Gandhi
Brijesh Gandhi

Reputation: 570

Please try this code on gridview's event OnRowDataBound="GridView_RowDataBound"

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.DataItem) != null)
        {
            Label lblStatusCode = (Label)e.row.FindControl("lblStatusCode");
            objJV.Status = Convert.ToInt32(lblStatusCode.Text);
        }
    }
}

Upvotes: 0

webnoob
webnoob

Reputation: 15934

The text is applied to the control on the page AFTER the code behind runs. Can't you set the text in the code behind?

Edit: You are setting the value of the label on the page i.e aspx / ascx using Container.DataItem but this value is set after the code behind has run. Basically, when the code behind looks at the control it's text property hasn't been set yet. Instead, add a DataRowBinding event to your GridView and set the lblStatusCode.Text in the event in the code behind.

Upvotes: 0

Related Questions