获取错误连接字符串属性尚未初始化 [英] getting error connection string property has not been initialized

查看:84
本文介绍了获取错误连接字符串属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在开发代码,以便以后在数据库中创建所有条目,我应该能够在网格中调用它们并进行编辑或删除.我执行时编写的代码由于连接字符串属性未初始化而显示错误.您能帮我在我的代码中更改的位置,以避免出现此错误

我的代码:

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
        '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

推荐答案

好像您是在Form_Load事件中调用数据库,但是直到您单击按钮后才初始化连接.
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.


这篇关于获取错误连接字符串属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆