Lakindu Nanayakkara
Lakindu Nanayakkara

Reputation: 51

How to add rows to a data table

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

Upvotes: 1

Views: 1829

Answers (4)

Samiey Mehdi
Samiey Mehdi

Reputation: 9414

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

Upvotes: 1

maccettura
maccettura

Reputation: 10818

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.

Upvotes: 0

ManojKanojiya
ManojKanojiya

Reputation: 1

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

Upvotes: 0

ojlovecd
ojlovecd

Reputation: 4892

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

Upvotes: 0

Related Questions