Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Right here goes a slight problem I'm having with SQL and vb.net... I am new to VB.net in the first place, and even newer to SQL Server bindings in Apps. I never really saw the viability of it. But now that my apps are growing and the data handling is getting more demanding it is a VERY viable thing for me to invest in.

 

Right back to the problem... I'm trying to bind a Treeview(trvClients) to a SQL Server table and field. The code I'm using keeps throwing out an error at me : "Error 91 - Object refrence not set to an instance of an object." I have ABSOLUTELY NO IDEA what that means. But here is the code as follows :

 

Private Sub BindToTreeSQL()

       Dim trvClients As New TreeView
       Dim varTvItem As New TreeNode
       Dim varDsRow As DataRow

       Try

           trvClients.Nodes.Clear()

           varTvItem = New TreeNode

           For Each varDsRow In SqlDsClients1.Tables("ClientName").Rows

               With varTvItem
                   .Text = varDsRow.Item("ClientName")
               End With
               trvClients.Nodes.Add(varTvItem)
           Next
       Catch ex As Exception
           'Error 91 : Object Variable Not Set 
           Dim msgb_ As String
           Dim errMsger As String, errMsger2 As String
           errMsger = Err.Number
           errMsger2 = Err.Description()
           msgb_ = MsgBox("Error :" & " " & errMsger & " " & errMsger2, vbOKOnly, "Error")
           Exit Sub
       End Try

   End Sub

 

in my form_load I have the following to bind the tree :

 

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

       'Get current logged on user's details
       sbpUser.Text = "Current User : " & System.Environment.UserName
       sbpStatus.Text = "Day Status : Not Started"

       'sbpTime.Text = Time.Now
       
       'trvClients binding to SQL
       BindToTreeSQL()

   End Sub

 

Thanx a million in advance.

P.L.U.R - Peace, Love, Unity, Respect :P
Posted

exception thrown here

 

It throws the exception on this line

For Each varDsRow In SqlDsClients1.Tables("ClientName").Rows

P.L.U.R - Peace, Love, Unity, Respect :P
Posted

Table is there

 

Yeah I have checked the dataset and the xsd file... it is in both of them. The table has been populated by field I entered in SQL Enterprise Manager for the time being. Once I have got the test data working and functioning in the application the SQL update commands etc will work no wirres as I have tested them already.

 

Someone mentioned to me I might have to declare the Object? But as I said earier I am new to VB.net and even newer to SQL databindings. Could you point me in the right direction if possible?

 

Thanx

P.L.U.R - Peace, Love, Unity, Respect :P
Posted

Solution to me own problem

 

Right so I figured it out EVENTUALLY - Rome wasn't built in a day after all... The code might be a little messy - but I'm still doing development on the app so can afford it... for now...

 

'Treeview Alpha list
       Dim i As Integer
       Dim node_letter As String
       trvClients.Nodes.Clear()

       'Set document type
       trvClients.Nodes.Add("Client List")

       'set documents
       trvClients.SelectedNode = trvClients.Nodes.Item(0)

       trvClients.ExpandAll()
       trvClients.ShowLines = True
       trvClients.ShowPlusMinus = True
       For i = 1 To 26
           node_letter = Chr(64 + i)
           trvClients.SelectedNode.Nodes.Add(node_letter)
       Next

       'Treeview Databinding
       Dim myrdr As SqlClient.SqlDataReader
       Dim node_key As String
       Dim entry_letter As String
       Dim node_name As String

       'set_NewTree()
       SqlCommand1.Connection.Open()
       myrdr = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)
       Do While myrdr.Read
           node_key = myrdr.GetString(1)
           entry_letter = Mid(myrdr.GetString(1), 1, 1)
           Dim MyNode As TreeNode
           i = 0
           node_name = myrdr.GetString(1) & ", " & myrdr.GetString(2)
           For Each MyNode In trvClients.Nodes.Item(0).Nodes
               If MyNode.Text = entry_letter Then
                   trvClients.Nodes.Item(0).Nodes.Item(MyNode.Index).Nodes.Add(node_name)
               End If
           Next
       Loop
       myrdr.Close()
       SqlConnection1.Close()

 

There you have it... If anyone has any sugestions to making the code tighter or imporvements - please don't hesitate to let me know. Thanx for all support

P.L.U.R - Peace, Love, Unity, Respect :P

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...