tags:

views:

221

answers:

6

I've created

        DataTable dt = new DataTable();

        dt.Columns.Add("Type");
        dt.Columns.Add("Address1");
        dt.Columns.Add("Address2");
        dt.Columns.Add("PostalCode");
        dt.Columns.Add("Country");

        DataRow drow = dt.NewRow();

        drow["Type"] = ddlAddressType.SelectedItem.ToString();
        drow["Address1"] = txtAddress1.Text;
        drow["Address2"] = txtAddress2.Text;
        drow["PostalCode"] = txtPostalCode.Text;
        drow["Country"] = ddlCountry.SelectedItem.ToString();

        dt.Rows.Add(drow);

        Session["Address"] = dt;

tried to add the value into a gridview using the following code:

public void populateAddressGridView()
{
    if (Session["Address"] != null)
    {
        DataTable dt = (DataTable)Session["Address"];

        if ((dt != null) && (dt.Rows.Count > 0))
        {
            AddressGridView.Visible = true;
            AddressGridView.DataSource = dt;
            AddressGridView.DataBind();
        }
        else
        {
            AddressGridView.Visible = false;
        }
    }

but the fields are empty without any value after adding a new data row. if i enable autogeneration of fields, i am able to view the generated data.

how do i go about solving this problem?

A: 

What's the definition of your gridview?

Steven Robbins
A: 

In the definition of your GridView make sure that you have your defined if you have AutoGenerateColumns set to false.

Aaron
A: 

Looks right to me. When you debug, can you confirm the session object is being cast correctly to a data table? Does dt have rows after the cast? Is the code getting to the databind method?

asp316
A: 

Hey all,

Is it necessary to register

Session["Address"] variable in Global.asax file before use it ???

and Hector,

make sure your application's session sate is Enabled on web.config file.

Viki
+1  A: 

Sounds to me like you need to set AutoGenerateColumns="false" and then specify the columns to show in the gridview:

<asp:GridView ID="AddressGridView" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField HeaderText="Type" DataField="Type" />
    <asp:BoundField HeaderText="Address1" DataField="Address1" />
    <asp:BoundField HeaderText="Address2" DataField="Address2" />
    <asp:BoundField HeaderText="PostalCode" DataField="PostalCode" />
    <asp:BoundField HeaderText="Country" DataField="Country" />
</Columns>
<EmptyDataTemplate>
    No records were found matching your search criteria
</EmptyDataTemplate>
</asp:GridView>
CRice
A: 

sorry for the late reply....

I've found my mistake, it's actually because i did not specify the data source of each column.

Thanks alot for everyone's reply =)