473,749 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Da tabases
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.SqlSe rver.Management .Smo
Imports Microsoft.SqlSe rver.Management .Common
Public Class Form1

'Public Class frmSQLConnectio n
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim objServers As DataTable
Dim strServer As String

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

For Each objRow As DataRow In objServers.Rows

strServer = CStr(objRow("Se rver"))
If Not TypeOf objRow("Instanc e") Is DBNull AndAlso
CStr(objRow("In stance")).Lengt h 0 Then

strServer += "\" & CStr(objRow("In stance"))

End If

Me.comboServers .Items.Add(strS erver)

Next

Me.ComboDatabas e.Enabled = False

End Sub

Private Sub button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click

Dim objConn As ServerConnectio n

If Me.comboServers .Text.Trim.Leng th() = 0 Then
objConn = New ServerConnectio n()

If Me.comboServers .Text.Trim.Leng th() 0 Then

objConn.ServerI nstance = 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.ComboDatabas e.Items.Clear()
For Each objDB As Database In Me.SMOServer.Da tabases
Me.ComboDatabas e.Items.Add(obj DB.Name)
Next

Me.ComboDatabas e.Enabled = True

Me.ComboDatabas e.SelectedIndex = 0


End Sub
Private Sub button2_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button2.Click

SMODatabase = Me.SMOServer.Da tabases(Me.Comb oDatabase.Text)

Me.DialogResult = Windows.Forms.D ialogResult.OK

Me.Close()

End Sub
Private Sub button3_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button3.Click

Me.DialogResult = Windows.Forms.D ialogResult.Can cel

Me.Close()

End Sub

End Class
Jun 27 '08 #1
4 967
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.Da tabases
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(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click

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

If Me.comboServers .Text.Trim.Leng th() 0 Then
This line can never be reached:
objConn.ServerI nstance = 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(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click

Dim objConn As ServerConnectio n

If Me.comboServers .Text.Trim.Leng th() = 0 Then
objConn = New ServerConnectio n()

If Me.comboServers .Text.Trim.Leng th() 0 Then

objConn.ServerI nstance = 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.ComboDatabas e.Items.Clear()
For Each objDB As Database In Me.SMOServer.Da tabases
Me.ComboDatabas e.Items.Add(obj DB.Name)
Next

Me.ComboDatabas e.Enabled = True

Me.ComboDatabas e.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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim objConn As ServerConnectio n

'If Me.comboServers .Text.Trim.Leng th() = 0 Then
objConn = New ServerConnectio n()

If Me.comboServers .Text.Trim.Leng th() 0 Then

objConn.ServerI nstance = 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.ComboDatabas e.Items.Clear()
For Each objDB As Database In Me.SMOServer.Da tabases
Me.ComboDatabas e.Items.Add(obj DB.Name)
Next

Me.ComboDatabas e.Enabled = True

Me.ComboDatabas e.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
2254
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 data: a= b= c= d=
28
20338
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
11
9267
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
29
2297
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
2515
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: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
16
2898
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
7
3305
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
3
2771
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? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions doesn't actually give any insight.
2
2654
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
12346
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
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.