title: Insert a record in sql database using vb.net datatable and datarow features
body: <p>I am trying to insert a record in sql database using vb.net dataadapter, datatable, and datarow features. I use the following code but it gives me an error: </p>
  
  <p><code>Object reference not set to an instance of an object</code></p>
  
  <pre><code>Imports System.Data.SqlClient
  Public Class Form1  
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
          Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
          Dim da As New SqlDataAdapter
          Dim ds As New DataSet
  
          Try
              cn.Open()
              da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
              da.Fill(ds) 
              Dim dt As New DataTable
              dt = ds.Tables("emp_tbl")
  
              'Error in this line(Object reference not set to an  instance of an object)'
              Dim dr As DataRow = dt.NewRow() 
  
              dr.Item("emp_id") = TextBox1.Text.Trim
              dr.Item("emp_name") = TextBox2.Text.Trim
              dr.Item("salary") = TextBox3.Text.Trim
              dr.Item("age") = TextBox4.Text.Trim
              dr.Item("emp_group") = TextBox5.Text.Trim
              dt.Rows.Add(dr)
              da.Update(ds)
              MsgBox("Record Successfully Inserted")
          Catch ex As Exception
              MsgBox(ex.Message)
          End Try
      End Sub
  End Class 
  </code></pre>