-1

Basically I am trying to develop a software and I am new in programming. I am trying to insert the data of textbox into SQL Server 2008 R2 Standard and I am getting an error:

System.NullReferenceException was unhandled

Here is my code.

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";

con.Open();

DataSet ds = new DataSet();

String sql = "Select * From tbl_songdb";

SqlDataAdapter da = new SqlDataAdapter(sql, con);

DataRow drow = ds.Tables["tbl_songdb"].NewRow();  // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;

ds.Tables["tbl_songdb"].Rows.Add(drow);
con.Close();
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • You don't have any code here that updates the database, just a `SELECT` statement. The `SqlDataAdapter` needs to be supplied with the SQL code to carry out `INSERT`s and `UPDATE`s. – Martin Aug 21 '16 at 07:41
  • This question may have the answer to your question: http://stackoverflow.com/questions/1631054/using-sqldataadapter-to-insert-a-row – Martin Aug 21 '16 at 07:43
  • i am using the codes written in the book and the same codes i can find on different sites which many developers are using but the same syntax are not working for me – Bhawesh Taunk Aug 21 '16 at 07:50
  • Is it that ds isn't assigned any values before then tryng to assign drow from ds.Tables? – MikeT Aug 21 '16 at 08:52

3 Answers3

1

actually my dataset was empty, thats why it was showing NULL error

 da.Fill(ds, "tbl_studentData");

i used these lines to fill it and now everything is working fine. thanks to all for giving their time.

0

Just do exactly what error said to you. Handle it with try catch like that:

      try{
    SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";
            con.Open();
            DataSet ds = new DataSet();
            String sql = "Select * From tbl_songdb";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataRow drow = ds.Tables["tbl_songdb"].NewRow();  // I am getting error message here.
            drow[1] = txt_songName.Text;
            drow[2] = txt_minute.Text;
            drow[3] = txt_albumnName.Text;
            drow[4] = txt_location.Text;
            ds.Tables["tbl_songdb"].Rows.Add(drow);
        da.Update(ds); 
    con.Close();
    }
    catch(Exception ex)
    {
MessageBox.Show(ex.ToString());

}
Whencesoever
  • 2,218
  • 15
  • 26
0
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";

con.Open();

DataSet ds = new DataSet();

String sql = "Select * From tbl_songdb";

SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
DataRow drow = ds.Tables[0].NewRow();  // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;

ds.Tables[0].Rows.Add(drow);
SQLiteCommandBuilder cmdbuilder = new SQLiteCommandBuilder(da);
da.InsertCommand = cmdbuilder.GetInsertCommand();
da.Update(ds);
ds.AcceptChanges();
con.Close();
  • Care to **explain** what you did? Don't just dump a pile of code on us and let us wonder what you might have done / changed / added !! .... – marc_s Aug 21 '16 at 09:48
  • sorry.... but its done and thank you very much for your time and next time i'll be more careful about this – Bhawesh Taunk Aug 21 '16 at 10:51
  • sorry its my mistake. I should explain it.... but its good to know that the problem is already solved. – Ali Muhammad Asif Aug 21 '16 at 21:27