Question : Filling a TextBox on a Form with Data from a Field from an SQL Database Table in Visual Studio 2008

I have a Form named  ViewNotes  in a Visual Basic Project in Visual Studio 2008. This form has a RichTextBox on it named txtNotes.
I am trying to load data from  “note”  Field of a Table named  t_notes  in an SQL Server Database named MyDatabase.
I  am trying to achieve this using code from a Book (Learn Visual Basic in 24 Hours by James Foxall  - I have to admit it is taking me more than 24 hours to achieve this)
The code provided in this book actually teaches you how to Access Data from an Access Database using ADO Net and Ole.OleDBConnection.
I am trying to modify this code to use it to Access Data  from an SQL Server using SQL Client.
I am providing this modified code in the code window.
This code however, is not working. It gives the following error
The SelectCommand Property has not been initialised  before Calling “Fill”
with debugger highlighting  the following line in my code:
     
 mySQLDataAdapterr.Fill(myDataTab)

I would be most grateful for advice on errors in this code which are preventing it from achieving its desired objective.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class ViewNotes
    Dim sqlConn As New SqlConnection

    Private mySQLConnection As New SqlConnection
    Private mySQLDataAdapter As SqlDataAdapter
    Private mySQLCommandBuilder As SqlCommandBuilder
    Private myDataTable As New DataTable
    Private RowPosition As Integer




    Private Sub ViewBookCode_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        mySQLConnection.Close()
        mySQLConnection.Dispose()


    End Sub



    Private Sub ViewBookCode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        mySQLConnection.ConnectionString = "Data Source=VISTA-ON-MAC\SQLEXPRESS;Initial Catalog=WriterDemo;Persist Security Info=True;User ID=sa;Password=peter;Connect Timeout=30"

        Dim customer_id As Integer
        customer_id = 1

        mySQLConnection.Open()

        mySQLDataAdapter = New SqlDataAdapter("SELECT * FROM t_notes WHERE customer_id =" & customer_id, sqlConn)


        mySQLDataAdapter = New SqlDataAdapter


        mySQLCommandBuilder = New SqlCommandBuilder(mySQLDataAdapter)
        mySQLDataAdapter.Fill(myDataTable)

        Me.ShowCurrentRecord()



    End Sub
    Private Sub ShowCurrentRecord()
        If myDataTable.Rows.Count = 0 Then
            txtNotes.Text = ""
            Exit Sub


        End If

        txtNotes.Text = myDataTable.Rows(RowPosition)("note").ToString()



    End Sub
End Class

Answer : Filling a TextBox on a Form with Data from a Field from an SQL Database Table in Visual Studio 2008

change the line no 33

        mySQLDataAdapter = New SqlDataAdapter("SELECT note FROM t_notes", mySQLConnection)

        Your current active connection is mySQLConnection not sqlconn. Complete Code that i run are thats with my database
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class ViewSQLClient

    Dim sqlConn As New SqlConnection
    Private mySQLConnection As New SqlConnection
    Private mySQLDataAdapter As SqlDataAdapter
    Private mySQLCommandBuilder As SqlCommandBuilder
    Private myDataTable As New DataTable
    Private RowPosition As Integer
    Private Sub ViewSQLClient_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        mySQLConnection.Close()
        mySQLConnection.Dispose()
    End Sub
    Private Sub ViewSQLClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mySQLConnection.ConnectionString = "Data Source=SMARTCHEATER\sqlexpress;Initial Catalog=cricket;Integrated Security=True"
        mySQLConnection.Open()
        mySQLDataAdapter = New SqlDataAdapter("SELECT name FROM countries", mySQLConnection)
        mySQLCommandBuilder = New SqlCommandBuilder(mySQLDataAdapter)
        ' The code in the next line gives the error 
        ' Error Messag:e the ConnectionString Property has not been initialised
        mySQLDataAdapter.Fill(myDataTable)
        Me.ShowCurrentRecord()
    End Sub
    Private Sub ShowCurrentRecord()
        If myDataTable.Rows.Count = 0 Then
            txtNotes.Text = ""
            Exit Sub
        End If
        txtNotes.Text = myDataTable.Rows(RowPosition)("name").ToString()
    End Sub
End Class
Random Solutions  
 
programming4us programming4us