0
votes

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...?

2
You need to explain where those lines of code in your CS resideMVCKarl
I have written on submit function to store that value into a table after click event of a button in the GridViewANIL
StatusCode column values are not null. But in lblStatusCode.Text has null.ANIL
See my answer it explains your problem.webnoob
Are you checking it in the datarow? or header row?Murali Murugesan

2 Answers

0
votes

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.

0
votes

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);
        }
    }
}