Example : How to save/store the data/record into sql server 2014 database in VB .net .
(Write/Put Connectivity Code here first.)

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click

    Dim rdb1 As String    
    Dim chk1 As String
    Dim chk2 As String

    If RdbGender1.Checked = True Then
       rdb1 = "Male"
    ElseIf RdbGender2.Checked = True Then
       rdb1 = "Female"
    End If

    If ChkMatric.Checked = True Then
       chk1= "Matric"
    End If
    If ChkInter.Checked = True Then
       chk2= "Inter"
    End If

    Dim k As Integer = MsgBox("Do You Want To Save!", MsgBoxStyle.YesNo, "Save Execpiton")
    If k = 6 Then
        Try
           cn.Open()
           cm.CommandText = "Insert into TableName(TxtName5, DtDob5, RdbGender5, ChkMatric5, 
           ChkInter5, TxtMobile5, CmbNational5, TxtUname5, TxtPass5, RtbAddress5)Values('" + 
           TxtName.Text + "','" + DtDob.Text + "','" + rdb1 .ToString + " ','" + chk1.ToString + " 
           ','" + chk2.ToString + " ','" + TxtMobile.Text + " ','" + CmbNational.Text + " ','" + 
           TxtUname.Text + " ','" + TxtPass.Text + " ','" + RtbAddress.Text + " ')"
           cm.ExecuteScalar()
           cn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
            Exit Sub
        Finally
            cn.Close()
        End Try
            MsgBox("RECORD SAVED SUCCESSFULLY")
    End If
End Sub

Example : How to stop duplicate data/record to save into sql server 2014 database in VB .net.
(Write/Put Connectivity Code here first.)

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
   Dim p1 As New Integer
        p1 = 0
        cn.Open()
        cm.CommandText = "select slno from polldtl"
        rdr = cm.ExecuteReader()
        While (rdr.Read())
            p1 = rdr("slno").ToString
            If p1 = TxtSlNo.Text Then
                MsgBox("Serial No. Already Exist, Add New data")
                TxtSlNo.Text= " "
                TxtSlNo.Focus()
                cn.Close()
                Exit Sub
            End If
        End While
        cn.Close()
End Sub
Example : How to Save the Data in Database from DataGridView in VB .net.
(Write/Put Connectivity Code here first.)

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
   Dim sql as String
   Dim result as Integer
   Try
      Cn.open
           For Each row As DataGridViewRow In DataGridViewRow1.Rows
              If row.Cells(0).FormattedValue <> "" Or row.Cells(1).FormattedValue <> "" Then
                 sql="INSERT INTO discount(sno,publisher,pubdis,cust,comdis)VALUES('" _
                        & CStr(row.Cells(0).FormattedValue) & "','" _
                        & CStr(row.Cells(1).FormattedValue) & "','" _
                        & CStr(row.Cells(2).FormattedValue) & "','" _
                        + CmbCust.Text + "','" + y + "')"

                        With cm
                            .Connection = cn
                            .CommandText = sql
                        End With
                        result = cm.ExecuteNonQuery
              End If
          Next

          If result = 0 Then
              MsgBox("Data is Not saved.")
          Else
              MsgBox("All Records are Saved.")
          End If
   Catch ex As Exception
        MsgBox(ex.Message)
   End Try
        cn.Close()
End Sub
Example : How to Save the Data in Database from text box, combo box etc. along with                                   Listview in VB .net.
(Write/Put Connectivity Code here first.)

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
   For i As Integer = 0 To ListView1.Items.Count - 1
      With ListView1.Items(i)
        Try
          cn.Open()
             cm.CommandText = "INSERT INTO SalesInvoiceTail (SEDate,CurrentTime,BillNo,SlNo,
                  ProCode,ProName,Qntty,Unit,Rate,Amount,Return) VALUES ('" + 
                  Me.DTPSEDate.Value.Date.ToShortDateString.ToString + " ','" + LblCurrentTime.Text 
                  + "'," + CmbBillNo.Text + "," + .SubItems(0).Text + "," + .SubItems(1).Text + ",'" 
                  + .SubItems(2).Text + "'," + .SubItems(3).Text + ",'" + .SubItems(4).Text + "'," + 
                  .SubItems(5).Text + "," + .SubItems(6).Text + ",'" + .SubItems(7).Text + "')"

                    cm.ExecuteScalar()
         Catch ex As Exception
             MessageBox.Show(ex.Message.ToString)
         Finally
             cn.Close()
         End Try
     End With
  Next
  MsgBox("RECORD SAVED SUCCESSFULLY")
End Sub
(Write/Put Connectivity Code here first.)

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click

   For i As Integer = 0 To ListView1.Items.Count - 1
       With ListView1.Items(i)
          Try
             cn.Open()
               cm.CommandText = "INSERT INTO address (roll, name, address)VALUES (" +.SubItems(0).
                    Text + ",'" + .SubItems(1).Text + "','" + .SubItems(2).Text + "')"
                        cm.ExecuteScalar()
          Catch ex As Exception
              MessageBox.Show(ex.Message.ToString)
          Finally
              cn.Close()
          End Try
       End With
   Next
   MessageBox.Show("Data inserted")
End Sub

-----------------  OR  ----------------

(Write/Put Connectivity Code here first.)
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click

  For i As Integer = 0 To Listview1.Count - 1
     cn.Open()
        cm.CommandText = "Insert into usereg values('" + djobmonthlist(i) + "'," + djobnolist(i) +  
             ",'" + bjobmonthlist(i) + "','" + cjobmonthlist(i) + "'," + cjobnolist(i) + ") "
        cm.ExecuteScalar()
     cn.Close()
  Next
End Sub

Loading

Categories: VB .Net Codes

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.