3

Can someone explain why i get this error message i c# :

Exception thrown: 'System.NullReferenceException' in systemutviklertest.exe Object reference not set to an instance of an object.

with this code

        string cs = "Database=db; Data Source=localhost;User=root;Password=pass;";
        MySqlConnection con = new MySqlConnection(cs);
        con.Open();

        string sql = "SELECT * FROM kunde";
        MySqlCommand cmd = new MySqlCommand(sql, con);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        MySqlCommandBuilder cb = new MySqlCommandBuilder(

        try
        {
            DataRow nrad;
            nrad = ds.Tables["kunde"].NewRow();
            nrad["firmanavn"] = firmanavn.Text;
            nrad["adresse"] = adresse.Text;
            nrad["postadresse"] = postadresse.Text;
            nrad["telefon"] = telefon.Text;
            nrad["fax"] = fax.Text;
            ds.Tables["kunde"].Rows.Add(nrad);
            da.Update(ds, "kunde");

        }

        catch (Exception s)
        {
            Console.WriteLine(s.Message);
        }

        con.Close();

The error shows up at "nrad = ds.Tables["kunde"].NewRow();

Sneezy
  • 193
  • 1
  • 11
  • I think you are still missing some code -- look at the end of the line with `MySqlCommandBuilder`... Also you aren't defining a `dataset` -- so that's a problem in itself. Look at this answer for the correct approach: http://stackoverflow.com/a/1631133/1073631 – sgeddes May 10 '16 at 20:52

0 Answers0