Hello Everyone
I am developing code to make all entries in database later i should be able to call them in grid and edit or delete. The code which i written when executing is showing error as connection string property has not been intialized. Can You help me where in my code i should change in order to avoid this error
My code:
Imports System.Data.SqlClient
Public Class Form1
Dim conn As New SqlConnection
Dim cmdStudent As New SqlCommand
Dim daStudent As New SqlDataAdapter
Dim dsStudent As New DataSet
Dim dtStudent As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
cmdStudent = conn.CreateCommand
cmdStudent.CommandText = "SELECT * FROM details"
daStudent.SelectCommand = cmdStudent
daStudent.Fill(dsStudent, "Student")
dgStudent.DataSource = dsStudent
dgStudent.DataMember = "Student"
dgStudent.ReadOnly = True
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
Dim myconnection As New SqlConnection("server=THEJASVI-PC\SQLEXPRESS;database=Student;uid=sa;pwd=2x12")
Dim mycommand As SqlClient.SqlCommand
myconnection.Open()
mycommand = New SqlCommand("insert into (id,FirstName,LastName,Age) values ('" & txtId.Text & "','" & txtFirstName.Text & "', '" & TextBox1.Text & "', '" & TxtAge.Text & "')", myconnection)
mycommand.ExecuteNonQuery()
MessageBox.Show("New Row Saved")
myconnection.Close()
txtId.Text = " "
txtFirstName.Text = " "
TextBox1.Text = " "
TxtAge.Text = " "
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit.Click
Dim check As Integer
Dim cmdStudent As New SqlCommand
Dim daStudent As New SqlDataAdapter
Dim dsStudent As New DataSet
If txtId.Text = "" Then
MessageBox.Show("Please fill all data!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
If MsgBox("Are you sure to edit Student data with Id : " & txtId.Text & " ?", MsgBoxStyle.OkCancel, "Edit confirm") = MsgBoxResult.Cancel Then
Else
Try
conn.Open()
cmdStudent = conn.CreateCommand
cmdStudent.CommandText = "UPDATE Student SET FirstName ='" & Trim(txtFirstName.Text) & "', LastName= '" & Trim(TxtLastName.Text) & "' , Age='" & Trim(TxtAge.Text) & "' WHERE Id ='" & Trim(txtId.Text) & "'"
check = cmdStudent.ExecuteReader.RecordsAffected
If check > 0 Then
MsgBox("Student With Id " & Trim(txtId.Text) & " Succesfully To Edit", MsgBoxStyle.OkOnly, "Info Update Data Student ")
Else
MsgBox("Student With Id " & Trim(txtId.Text) & " Failure To Edit", MsgBoxStyle.OkOnly, "Info Update Data Student ")
End If
conn.Close()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
End Try
End If
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete.Click
Dim check As Integer
Dim myconnection As New SqlConnection("server=THEJASVI-PC\SQLEXPRESS;database=Student;uid=sa;pwd=2x12")
Dim cmdStudent As New SqlCommand
Dim daStudent As New SqlDataAdapter
Dim dsStudent As New DataSet
If txtId.Text <> "" Then
If MsgBox("Are you sure to delete data with Id : " & txtId.Text & " ?", MsgBoxStyle.OkCancel, "Delete confirm") = MsgBoxResult.Cancel Then
Else
Try
myconnection.Open()
cmdStudent = myconnection.CreateCommand
cmdStudent.CommandText = "DELETE FROM Student WHERE Id ='" & Trim(txtId.Text) & "'"
check = cmdStudent.ExecuteReader.RecordsAffected
If check > 0 Then
MsgBox("Student With Id " & Trim(txtId.Text) & " Succesfully To Delete", MsgBoxStyle.OkOnly, "Info Delete Student")
Else
MsgBox("Student With Id " & Trim(txtId.Text) & " Failure To Delete", MsgBoxStyle.OkOnly, "Info Delete Student")
End If
myconnection.Close()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
End Try
End If
Else
MsgBox("fill Id Student on Id textbox which student to delete!!", MsgBoxStyle.OkOnly, "Info Data")
End If
End Sub
End Class
Awaiting for suggestion....
Thank You