Reputation: 131
Im working on a website but the problem/error is when i click on the add button it should add a Row in my Categories Table but it is giving me an error.
This is my Code: i have made the connection in gridview.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Category : System.Web.UI.Page
{
DAL obj = new DAL();
static DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void add_btn_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["Cname"] = cat_txt.Text;
dt.Rows.Add(dr);
obj.UpdateDataTable(dt);
}
}
and it's giving me this error An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code. Additional information: Column 'Cname' does not belong to table .
on this code "dr["Cname"] = cat_txt.Text"
i look into the error but i couldn't resolved it and i search it on google too but i couldn't find the exact solution to my error. Im stuck between this line of code so if you guys will help me it'll be really grateful..:)
Upvotes: 0
Views: 766
Reputation: 3644
this is solves your problem
dt.Columns.Add(new DataColumn("Cname"));
DataRow dr = dt.NewRow();
dr["Cname"] = cat_txt.Text;
dt.Rows.Add(dr);
why is this static? The answer is more important.
static DataTable dt = new DataTable();
Upvotes: 2