title: vb.net how do i add a new rows (and update) from datagridview programmatically to sql database
body: <p>asking a new question with regards to my previous problem.</p>
  
  <p>so this is how it goes,
  so far i was able make the following code to work (thanks to @JADE for helping me out, (a lot ) :)</p>
  
  <pre><code>Dim connstr As String = "server=midtelephone\sqlexpress; database=testdb; user= sa; password=sa;"
  
      cmdconn = New SqlConnection
      cmd = New SqlCommand        
      cmdconn.ConnectionString = connstr 
      cmd.Connection = cmdconn
      cmdconn.Open()
  
      Dim period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED, DIR_NO As String
      For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
  
  
          With Me.DataGridView1.Rows(i)
  
              If IsDBNull(.Cells(0).Value()) OrElse .Cells(0).Value() Is Nothing OrElse .Cells(0).Value().ToString().Trim() = "" Then
                  period = ""
              Else
                  period = .Cells(0).Value()
              End If
              If IsDBNull(.Cells(1).Value()) OrElse .Cells(1).Value() Is Nothing OrElse .Cells(1).Value().ToString().Trim() = "" Then
                  VOUCH_AMT = "0"
              Else
                  VOUCH_AMT = .Cells(1).Value()
              End If
              If IsDBNull(.Cells(2).Value()) OrElse .Cells(2).Value() Is Nothing OrElse .Cells(2).Value().ToString().Trim() = "" Then
                  INDIVIDUAL_AMT = "0"
              Else
                  INDIVIDUAL_AMT = .Cells(2).Value()
              End If
              If IsDBNull(.Cells(3).Value()) OrElse .Cells(3).Value() Is Nothing OrElse .Cells(3).Value().ToString().Trim() = "" Then
                  check_no = ""
              Else
                  check_no = .Cells(3).Value()
              End If
              If IsDBNull(.Cells(4).Value()) OrElse .Cells(4).Value() Is Nothing OrElse .Cells(4).Value().ToString().Trim() = "" Then
                  D_MAILED = ""
              Else
                  D_MAILED = .Cells(4).Value()
              End If
              If IsDBNull(.Cells(5).Value()) OrElse .Cells(5).Value() Is Nothing OrElse .Cells(5).Value().ToString().Trim() = "" Then
                  DIR_NO = ""
              Else
                  DIR_NO = .Cells(5).Value()
              End If
  
  
          End With
  
          cmd.CommandText = "insert into tobee.EBD_BILLHISTORY(period, vouch_amt, individual_amt, check_no, d_mailed, dir_no)values" &amp; _
              "('" &amp; period.Replace("'", "''") &amp; "'," &amp; VOUCH_AMT &amp; "," &amp; INDIVIDUAL_AMT &amp; ",'" &amp; check_no.Replace("'", "''") &amp; "','" &amp; D_MAILED.Replace("'", "''") &amp; "', '" &amp; DIR_NO.Replace("'", "''") &amp; "')"
          cmd.ExecuteNonQuery()
          MsgBox("Saved")
      Next
      cmdconn.Close()
  
  End Sub
  </code></pre>
  
  <p>now the problem is if i want to add new rows programmatically (i already set the dgv to edit programmatically ). what are the metrics or the next procedures to do? i have an edit button which enables the datagridview to be edited, something like this:</p>
  
  <pre><code>Private Sub btnEditmain_Click(sender As Object, e As EventArgs) Handles btnEditmain.Click
  
      DataGridView1.AllowUserToAddRows = True
      DataGridView1.BeginEdit(True)
      btnSave.Enabled = True
  
  End Sub
  </code></pre>
  
  <p>( also, as i check the table in my database, it seems that there's no changes made after clicking the save button ) pls. help me. thanks in advance. :)</p>