1

I have created a datatable globaly and i have add columns to it in the page load event. Now i want to add data to it in a button click event.. When I do it as below I get a error saying....

Column 'catID' does not belong to table

What is the solution... Do i need to use sessions... ? the code is like below

    public partial class Default2 : System.Web.UI.Page
{
    DataTable dtSelectedSeats = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dtSelectedSeats.Columns.Add("catID", typeof(string));
            dtSelectedSeats.Columns.Add("seatID", typeof(string));
        }

    }
    protected void seat_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender;

        if (button.BackColor == Color.Cyan)
        {
            button.BackColor = Color.Lime;

            addSeat(button.Text);
        }

    }

    private void addSeat(string seatNo)
    {
        DataRow dr;
        dr = dtSelectedSeats.NewRow();
        dr["catID"] = ddlCategory.SelectedItem.Value.ToString();
        dr["seatID"] = seatNo;
        dtSelectedSeats.Rows.Add(dr);
    }
}

4 Answers 4

1

ASPX:

<asp:DropDownList ID="ddlCategory" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="seat" runat="server" BackColor="Cyan" onclick="seat_Click" Text="1" />
<asp:Button ID="Button1" runat="server" BackColor="Cyan" onclick="Button1_Click" Text="2" />
<asp:Button ID="Button2" runat="server" BackColor="Cyan" onclick="Button2_Click" Text="3" /><br />
<asp:GridView ID="GridView1" runat="server"/>

Code behind:

protected void seat_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    if (button.BackColor == Color.Cyan)
    {
        button.BackColor = Color.Lime;
        addSeat(button.Text);
    }
}
private void addSeat(string seatNo)
{
    if (Session["dt"] == null)
    {
        Response.Write("DataTable not exist!");
        return;
    }
    DataTable dtSelectedSeats = (DataTable)Session["dt"];
    DataRow dr = dtSelectedSeats.NewRow();
    dr["catID"] = ddlCategory.SelectedItem.Value.ToString();
    dr["seatID"] = seatNo;
    dtSelectedSeats.Rows.Add(dr);
    GridView1.DataSource = dtSelectedSeats;
    GridView1.DataBind();
    Session["dt"] = dtSelectedSeats;
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dtSelectedSeats = new DataTable();
        dtSelectedSeats.Columns.Add("catID", typeof(string));
        dtSelectedSeats.Columns.Add("seatID", typeof(string));
        Session["dt"] = dtSelectedSeats;
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    seat_Click(sender, e);
}
protected void Button2_Click(object sender, EventArgs e)
{
    seat_Click(sender, e);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Does it means i have to use Sessions ?
You can use Session["dt"] or ViewState["dt"]. if you uses only one page ViewState is best way but if you use multiple pages Session is best way.
0

just remove the if (!IsPostBack) coz when you click the button , the page will post back.

1 Comment

It works.. but...When i click the first button.. it added to the datatable.. when i click another button the datatable refreshes and the previous data have lost.. I want to keep the previous records and add the new record to the data table
0

Your Code is perfectly correct for DataTable, the error you are getting may be for some different problem. Please specify correctly your error

1 Comment

Please, try to read this stackoverflow.com/about, to get more understanding about questions/answers here on SO. Your contribution is not answering the question. It is more a comment, which you can add once you'll increase your reputation: stackoverflow.com/faq#reputation
0

Just off the top of my head it looks as if you are adding the rows in the wrong event. I'm not sure your DataTable has been initialized at the point you are adding columns to it (thus throwing away your changes). Try putting your PageLoad code into PagePrerender and see if that gives you a better result.

Comments

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.