Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
VB
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
        'Dim dr As SqlDataReader
        'Dim dr1 As SqlDataReader

        myconnection.Open()
        'Dim s As String
        's = Format(dtSdate.Text, "yyyy-mm-dd")
        's = Format(dtSdate.Value, "yyyy-MM-dd")
        'MsgBox(s, MsgBoxStyle.DefaultButton1)
        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)
            ' Else
            '    If txtId.Text = "" Or txtFirstName.Text = "" TextBox1.Text = "" or txtAge.Text = ""  Then


            '   MsgBox("Student Data is not completed", MsgBoxStyle.OkOnly)
            'Else
            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 If
        'End If
        ' do nothing
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete.Click
        Dim check As Integer
        'Dim conn As System.Data.SqlClient.SqlConnection
        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
                    'conn.Open()
                    myconnection.Open()

                    'cmdStudent = conn.CreateCommand
                    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
                    'conn.Close()
                    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
Posted
Updated 18-Aug-10 1:46am
v2

1 solution

Looks like you're making a call to the database in the Form_Load event but not initializing the connection until you click a button.
 
Share this answer
 
Comments
[no name] 18-Aug-10 14:54pm    
Reason for my vote of 5
Good answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900