473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

object reference not set

TG
hi!

I have a combobox where the user can type the sql server name. Then a
connect button that, when the user click, I want it to populate
another combobox with all the databases from the server in the
previous combobox.

Unfortunately, i am getting the error object reference is not set....!

Here is where i am getting the error:

For Each objDB As Database In Me.SMOServer.Databases
I am new to VB ...I'm using vb2008 connecting to sql server 2005.
Your help is appreciated.

Thanks!

Tammy


Here is my complete code:

Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Public Class Form1

'Public Class frmSQLConnection
Private m_objServer As Server
Public Property SMOServer() As Server
Get
Return m_objServer
End Get
Private Set(ByVal value As Server)
m_objServer = value
End Set
End Property

Private m_objDatabase As Database
Public Property SMODatabase() As Database
Get
Return m_objDatabase
End Get
Private Set(ByVal value As Database)
m_objDatabase = value
End Set
End Property

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

Dim objServers As DataTable
Dim strServer As String

'---- retrieve a list of SQL Server instances on the network
objServers = SmoApplication.EnumAvailableSqlServers(False)

For Each objRow As DataRow In objServers.Rows

strServer = CStr(objRow("Server"))
If Not TypeOf objRow("Instance") Is DBNull AndAlso
CStr(objRow("Instance")).Length 0 Then

strServer += "\" & CStr(objRow("Instance"))

End If

Me.comboServers.Items.Add(strServer)

Next

Me.ComboDatabase.Enabled = False

End Sub

Private Sub button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim objConn As ServerConnection

If Me.comboServers.Text.Trim.Length() = 0 Then
objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() 0 Then

objConn.ServerInstance = Me.comboServers.Text.Trim()

End If
Me.SMOServer = New Server(objConn)

End If
'---- Note: the connection will open when we call our first
method on the Server object
Me.ComboDatabase.Items.Clear()
For Each objDB As Database In Me.SMOServer.Databases
Me.ComboDatabase.Items.Add(objDB.Name)
Next

Me.ComboDatabase.Enabled = True

Me.ComboDatabase.SelectedIndex = 0


End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click

SMODatabase = Me.SMOServer.Databases(Me.ComboDatabase.Text)

Me.DialogResult = Windows.Forms.DialogResult.OK

Me.Close()

End Sub
Private Sub button3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click

Me.DialogResult = Windows.Forms.DialogResult.Cancel

Me.Close()

End Sub

End Class
Jun 27 '08 #1
4 946
TG wrote:
hi!

I have a combobox where the user can type the sql server name. Then a
connect button that, when the user click, I want it to populate
another combobox with all the databases from the server in the
previous combobox.

Unfortunately, i am getting the error object reference is not set....!

Here is where i am getting the error:

For Each objDB As Database In Me.SMOServer.Databases
I am new to VB ...I'm using vb2008 connecting to sql server 2005.
Your help is appreciated.

Thanks!

Tammy
8<
>
Private Sub button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim objConn As ServerConnection
You only assign anything to the SMOServer property if the content of the
combobox is empty:
If Me.comboServers.Text.Trim.Length() = 0 Then
objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() 0 Then
This line can never be reached:
objConn.ServerInstance = Me.comboServers.Text.Trim()

End If
Me.SMOServer = New Server(objConn)

End If

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
If I'm reading the code below correctly, SMOServer is only set if the
combobox text length is zero. That doesn't sound likely or correct. Inside
that if block you test the length greater than zero. I think your if block
needs to be reworked.

Private Sub button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim objConn As ServerConnection

If Me.comboServers.Text.Trim.Length() = 0 Then
objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() 0 Then

objConn.ServerInstance = Me.comboServers.Text.Trim()

End If
Me.SMOServer = New Server(objConn)

End If
'---- Note: the connection will open when we call our first
method on the Server object
Me.ComboDatabase.Items.Clear()
For Each objDB As Database In Me.SMOServer.Databases
Me.ComboDatabase.Items.Add(objDB.Name)
Next

Me.ComboDatabase.Enabled = True

Me.ComboDatabase.SelectedIndex = 0


End Sub
Jun 27 '08 #3
TG
How should I re-write it?

Like I said i am new to VB and I found something similar to this which
I was trying to modify to get what I need.

Thanks a lot for your help guys!

Tammy

Jun 27 '08 #4
TG
I got it!

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objConn As ServerConnection

'If Me.comboServers.Text.Trim.Length() = 0 Then
objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() 0 Then

objConn.ServerInstance = Me.comboServers.Text.Trim()

End If
Me.SMOServer = New Server(objConn)

'End If
'---- Note: the connection will open when we call our first
method on the Server object
Me.ComboDatabase.Items.Clear()
For Each objDB As Database In Me.SMOServer.Databases
Me.ComboDatabase.Items.Add(objDB.Name)
Next

Me.ComboDatabase.Enabled = True

Me.ComboDatabase.SelectedIndex = 0


I commented out this line and now I get the results I was looking
for!!!!!

Thanks a lot for pointing me in the right direction!!!! :-)
Tammy

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
29
by: web1110 | last post by:
If I have 2 variables, A and B, referencing the same object and then do a A.Dispose(), what happens to B?
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.