title: c# writing to database, instance of an object
body: <p>I'm programming my first c# application, which has to connect to database. I want to save data from TextBoxes to database, but I have problem in marked line.</p>
  
  <p>Code for connecting to database:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace prviTelefonskiImenik
  {
      class DatabaseConnection
      {
          private string sql_string;
          private string strCon;
          System.Data.SqlClient.SqlDataAdapter da_1;
  
          public string Sql
          {
              set { sql_string = value; }
          }
  
          public string connection_string
          {
              set { strCon = value; }
          }
  
          public System.Data.DataSet GetConnection
          {
              get 
              { return MyDataSet(); }
          }
  
          private System.Data.DataSet MyDataSet()
          {
              System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
  
              con.Open();
  
              da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
  
              System.Data.DataSet dat_set = new System.Data.DataSet();
              da_1.Fill(dat_set, "Table_Data_1");
              //da_1.Fill(dat_set, "kontakti");
              con.Close();
  
              return dat_set;
          }
  
          public void UpdateDatabase(System.Data.DataSet ds)
          {
              System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
              cb.DataAdapter.Update(ds.Tables[0]);
          }
      }
  }
  </code></pre>
  
  <p>And code for saving data from TextBoxes to database:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace prviTelefonskiImenik
  {
      public partial class dodajKontakt : Form
      {
          public dodajKontakt()
          {
              InitializeComponent();
          }
  
          DatabaseConnection objConnect;
          string conString;
  
          DataSet ds;
          DataRow dRow;
  
          int MaxRows;
          int inc = 0;
  
          private void dodajKontakt_Load(object sender, EventArgs e)
          {
              try
              {
                  objConnect = new DatabaseConnection();
                  conString = Properties.Settings.Default.kontaktiConnectionString;
  
                  objConnect.connection_string = conString;
                  objConnect.Sql = Properties.Settings.Default.SQL;
  
                  ds = objConnect.GetConnection;
                  MaxRows = ds.Tables[0].Rows.Count;
  
                  NavigateRecords();
              }
              catch (Exception err)
              {
                  MessageBox.Show(err.Message);
              }
          }
  
          private void NavigateRecords()
          {
              dRow = ds.Tables[0].Rows[inc];
              txtIme.Text = dRow.ItemArray.GetValue(1).ToString();
              txtPriimek.Text = dRow.ItemArray.GetValue(2).ToString();
              txtEmso.Text = dRow.ItemArray.GetValue(3).ToString();
              txtTelefon.Text = dRow.ItemArray.GetValue(4).ToString();
              txtEmail.Text = dRow.ItemArray.GetValue(5).ToString();
          }
  
          private void shrani_Click(object sender, EventArgs e)
          {
  
              **DataRow row = ds.Tables[0].NewRow();**
              row[1] = txtIme.Text;
              row[2] = txtPriimek.Text;
              row[3] = txtEmso.Text;
              row[4] = txtTelefon.Text;
              row[5] = txtEmail.Text;
  
              ds.Tables[0].Rows.Add(row);
  
              try
              {
                  objConnect.UpdateDatabase(ds);
                  MaxRows = MaxRows + 1;
                  inc = MaxRows - 1;
                  MessageBox.Show("Database updated");
              }
              catch (Exception err)
              {
                  MessageBox.Show(err.Message);
              }
          }
      }
  }
  </code></pre>
  
  <p>While compiling, Visual Studio 2013 finds an error here: <strong>DataRow row = ds.Tables[0].NewRow();</strong>
  Additional information: Object reference not set to an instance of an object.</p>
  
  <p>Could you please help me to solve a problem? Thanks!</p>