I have vb.net application with SQL server express.
Here is my code to save data to database, but itn not working.
Here ID is not the primary key.
Imports System.Data.SqlClient
Imports System.Data
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim cmd As New SqlCommand
Dim con As New SqlConnection("Data Source=SIS-JES4MPANRS0\SQLEXPRESS;Initial Catalog=Pathology;Integrated Security=True")
con.Open()
Dim cmdlogin As New SqlCommand(("SELECT ID FROM Accessonbook-1"), con)
Dim dalogin As New SqlDataAdapter
Dim dslogin As New DataSet
dalogin.SelectCommand = cmdlogin
cmdlogin.ExecuteNonQuery()
dslogin.Clear()
dalogin.Fill(dslogin, "Accessonbook-1")
If dslogin.Tables(0).Rows.Count > 0 Then
Dim i As Integer = 0
While i < dslogin.Tables(0).Rows.Count
If IDtxt.Text = dslogin.Tables(0).Rows(i).Item(0).ToString Then
MsgBox("ID Already exist")
GoTo Line1
Else
End If
i = i + 1
End While
Else
MsgBox("No Data Exist")
End If
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into Accessonbook-1 (ID,Name,Age,Sex) Values ('" & IDtxt.Text & "','" & Nametxt.Text & "','" & Agetxt.Text & "','" & Sextxt.Text & "')"
cmd.ExecuteNonQuery()
MsgBox("Added Successfuly")
Line1:
End Sub