Hi All: I am Creating a Windows application using VB.Net. In which i have written code to insert data into SQL Server using listview in VB 2008 and Showing msg as successfully inserted. But not inserting data into the database. Kindly correct my code.
Private Sub btnsaveBill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsaveBill.Click
If cn.State = ConnectionState.Open Then
cn.Close()
End If
If cbcname.Text = Nothing Or ListView1.Items.Count = 0 Or txttotal.Text = Nothing Then
MsgBox("Cant save incomplete information", MsgBoxStyle.Critical, Me.Text)
Exit Sub
End If
Try
Dim i As Integer = 0
Dim rs1, rs2 As String
cn.Open()
rs1 = "INSERT INTO Billinfo (Cust_name, Cust_Mobile, Flddate, Sub_total, Tax_per, Tax, Total) VALUES('" & cbcname.Text & "','" & txtMobileno.Text & "','" & dpdate.Text & "','" & txtsubtot.Text & "','" & txtper.Text & "','" & txttax.Text & "','" & txttotal.Text & "')"
cmd.ExecuteNonQuery()
cn.Close()
cbcname.Text = ""
txtMobileno.Text = ""
dpdate.Text = ""
txtsubtot.Text = ""
txtper.Text = ""
txttax.Text = ""
txttotal.Text = ""
ListView1.Items.Clear()
cn.Close()
For i = 0 To ListView1.Items.Count = -1
cn.Open()
rs2 = "INSERT INTO Item_Sold (Item_name, Item_Make, Item_Srno, Unit_Price, Unit_Quantity, Itotal) VALUES('" & ListView1.Items(i).SubItems(1).Text & "','" & ListView1.Items(i).SubItems(2).Text & "','" & ListView1.Items(i).SubItems(3).Text & "','" & ListView1.Items(i).SubItems(4).Text & "','" & ListView1.Items(i).SubItems(5).Text & "','" & ListView1.Items(i).SubItems(6).Text & "')"
cn.Close()
Next
MsgBox("Saved Successfully")
BtnPrintBill.Enabled = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnsaveBill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsaveBill.Click
If cn.State = ConnectionState.Open Then
cn.Close()
End If
If cbcname.Text = Nothing Or ListView1.Items.Count = 0 Or txttotal.Text = Nothing Then
MsgBox("Cant save incomplete information", MsgBoxStyle.Critical, Me.Text)
Exit Sub
End If
Try
Dim i As Integer = 0
Dim rs1, rs2 As String
cn.Open()
rs1 = "INSERT INTO Billinfo (Cust_name, Cust_Mobile, Flddate, Sub_total, Tax_per, Tax, Total) VALUES('" & cbcname.Text & "','" & txtMobileno.Text & "','" & dpdate.Text & "','" & txtsubtot.Text & "','" & txtper.Text & "','" & txttax.Text & "','" & txttotal.Text & "')"
cmd.ExecuteNonQuery()
cn.Close()
cbcname.Text = ""
txtMobileno.Text = ""
dpdate.Text = ""
txtsubtot.Text = ""
txtper.Text = ""
txttax.Text = ""
txttotal.Text = ""
ListView1.Items.Clear()
cn.Close()
For i = 0 To ListView1.Items.Count = -1
cn.Open()
rs2 = "INSERT INTO Item_Sold (Item_name, Item_Make, Item_Srno, Unit_Price, Unit_Quantity, Itotal) VALUES('" & ListView1.Items(i).SubItems(1).Text & "','" & ListView1.Items(i).SubItems(2).Text & "','" & ListView1.Items(i).SubItems(3).Text & "','" & ListView1.Items(i).SubItems(4).Text & "','" & ListView1.Items(i).SubItems(5).Text & "','" & ListView1.Items(i).SubItems(6).Text & "')"
cn.Close()
Next
MsgBox("Saved Successfully")
BtnPrintBill.Enabled = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub