I made an online application to store employees information in MS SQL,I used Disconnected model and I made my class for "DataSet configuration" and call method in it in The "Form_Load" event. This is the class

public static string constr = ConfigurationManager.ConnectionStrings["cnn1"].ConnectionString;

public static DataSet GetDataSet(string stored_name, string table_name, params SqlParameter[] prmarr)
{
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand(stored_name, con);
    foreach (SqlParameter prm in prmarr)
    {
        cmd.Parameters.Add(prm);
    }
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, table_name);

    return ds;

I set the Connection string in "Web.Config"

Till now all things is ok but when I insert data to "DataSet" Error message appear

Object reference not set to an instance of an object.
Line 42: DataRow drnew = ds.Tables["x"].NewRow();

this is the code I Type in insert Button

DataRow drnew = ds.Tables["x"].NewRow();
drnew["First_Name"] = txtfname.Text;
drnew["Last_Name"] = txtlname.Text;
drnew["Country"] = drpboxcoun.SelectedValue;
drnew["Email"] = txtemail.Text;
drnew["Experience_Years"] = txtexper.Text;
drnew["Current_Salary"] = txtcursal.Text;
drnew["Expected_Salary"] = txtexpsal.Text;

ds.Tables["x"].Rows.Add(drnew);

I check the types compatible with fields in my database. so where is the problem !!

did you initialize the dataset before using it?

Have to initialize the dataset or directly DataTable with the column name and its type.

 DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof (string));
dt.Columns.Add("Education", typeof (string));
dt.Columns.Add("Location",typeof(string));

DataRow dtrow = dt.NewRow();    // Create New Row
dtrow["UserId"] = 1;            //Bind Data to Columns
dtrow["UserName"] = "SureshDasari";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Chennai";
dt.Rows.Add(dtrow);

gvDetails.DataSource = dt;
gvDetails.DataBind();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.