I'm trying to save data from textboxes in visual studio using vb.net to sql server 2008. the coonectiong is working perfectly but is not displaying the data output in data grid. This is the part of the code.
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Dim MyConnObj As New SqlClient.SqlConnection
MyConnObj.ConnectionString = "Data Source=VCPTSTDST01PC16\SQLEXPRESS;Initial Catalog = MzanziDBSQL;Integrated Security=SSPI"
MyConnObj.Open()
Dim sqlStr, sqlStrCheck As String
sqlStr = "INSERT INTO Patient(PatientID,Title,FirstName,LastName,Gender,IDNumber,Doctor,Phone,City,Address,PostalCode,Province,Country,Mobile,MedicalAid) VALUES(@PatientID,@Title,@FirstName,@LastName,@Gender,@IDNumber,@Doctor,@Phone,@City,@Address,@PostalCode,@Province,@Country,@Mobile,@MedicalAid)"
sqlStrCheck = "Select PatientID from Patient Where PatientID = '" + txtPatientID.Text + "'"
Dim sqlComm2 As New SqlCommand(sqlStrCheck, MyConnObj)
Dim dr As SqlDataReader = sqlComm2.ExecuteReader()
If Not dr.HasRows Then
Dim sqlComm As New SqlCommand(sqlStr, MyConnObj)
dr.Close()
Dim errorMessages As New StringBuilder()
Try
sqlComm.ExecuteNonQuery()
Catch ex As SqlException
Dim i As Integer
For i = 0 To ex.Errors.Count - 1
errorMessages.Append("Index #" & i.ToString() & ControlChars.NewLine _
& "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
& "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
& "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
& "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
Next i
Console.WriteLine(errorMessages.ToString())
End Try
MyConnObj.Close()
MessageBox.Show("New Patient has been added to the database")
Me.Close()
Else
MessageBox.Show("Patient already exists in database")
End If