hello..

my problem is, im using the following code to insert the records..but whenevr i click on ok button it shows the error "Object reference not set to an instance of an object"

im declaring the variables in the following form,

private OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ..\\abc.mdb");
        OleDbDataAdapter da;
        OleDbCommand cmd;
        DataSet ds;
        DataRow row;

and the code on the save button is

try
                {

                    cmd = new OleDbCommand("insert into info(name,description) values('" + txtgname.Text + "','" + txtdescription.Text + "')", cn);
                    //ds = new DataSet();
                    da = new OleDbDataAdapter(cmd);
                    da.InsertCommand = cmd;
                    da.Fill(ds, "info");

                    row = ds.Tables["info"].NewRow();
                    row["name"] = txtgname.Text;
                    row["description"] = txtdescription.Text;

                    ds.Tables[0].Rows.Add(row);
                    da.Update(ds.Tables[0]);
                    ds.AcceptChanges();


                    if (cn.State.ToString() == "Open")
                        cn.Close();
                }
                catch (Exception e1)
                {
                   MessageBox.Show("Error in info " + e1.Message);
                }

the database gets updated even when the error occurs.
what i need to do to stop the error.

Its require parameterized query.

da = new OleDbDataAdapter("select * from tabname",con);
da.InsertCommand = new OledBCommand("insert into tablename values (?,?)",con);
da.InsertCommand.Parameters.Add(....);
.....

Hi
You haven't made an object of DataRow before using row in your code.Insert
row = new DataRow();
before assigning row to anything.
Same for the DataSet also.1st make an object from DataSet.

This article has been dead for over six months. Start a new discussion instead.