Groups | Blog | Home
all groups > vb.net data > june 2005 >

vb.net data : I am doing something wrong (ADO.NET)


Rob
6/20/2005 12:26:13 AM
I am trying to get an example from a book to work...

I am new to dot net so it is likely I am doing something wrong.

I added a new module and created the function below which is run from a
button. When I click the button, nothing hapens (or at least I do not see
anything happen).

Function GrabData()

Dim strSQL As String

Dim strConn As String = "Provider=SQLOLEDB;Data Source=(local);" & _

"Initial Catalog=Northwind;Trusted_Connection=Yes;"

Dim cn As New OleDbConnection(strConn)

cn.Open()

strSQL = "Select CustomerID , CompanyName from Customers"

Dim cmd As New OleDbCommand(strSQL, cn)

Dim rdr As OleDbDataReader = cmd.ExecuteReader()

While rdr.Read()

Console.WriteLine(rdr("CustomerID") & " - " & rdr("CompanyName"))

End While

rdr.Close()



End Function


Ken Tucker [MVP]
6/20/2005 4:58:08 AM
Hi,

You are writing the output to the console. It will create a
console window and close it as soon as it is done. Try writing to the
output window with trace.writeline instead. Dont forget to close the
connection when you are done with it.


While rdr.Read()

Trace.WriteLine(rdr("CustomerID") & " - " & rdr("CompanyName"))

End While

rdr.Close()

cn.close

Ken
------------------------

[quoted text, click to view]
I am trying to get an example from a book to work...

I am new to dot net so it is likely I am doing something wrong.

I added a new module and created the function below which is run from a
button. When I click the button, nothing hapens (or at least I do not see
anything happen).

Function GrabData()

Dim strSQL As String

Dim strConn As String = "Provider=SQLOLEDB;Data Source=(local);" & _

"Initial Catalog=Northwind;Trusted_Connection=Yes;"

Dim cn As New OleDbConnection(strConn)

cn.Open()

strSQL = "Select CustomerID , CompanyName from Customers"

Dim cmd As New OleDbCommand(strSQL, cn)

Dim rdr As OleDbDataReader = cmd.ExecuteReader()

While rdr.Read()

Console.WriteLine(rdr("CustomerID") & " - " & rdr("CompanyName"))

End While

rdr.Close()



End Function



Rob
6/20/2005 8:47:18 AM
Hi Ken,

I tried that but I still see nothing...

Do I need to have it bound to a control on the form ?

Thanks,
Rob



[quoted text, click to view]

Ken Tucker [MVP]
6/20/2005 5:40:43 PM
Hi,

Dim conn As SqlConnection
Dim strConn As String
Dim dr As SqlDataReader
Dim cmd As SqlCommand
Dim strSql As String

strConn = "Server = (local);"
strConn &= "Database = NorthWind;"
strConn &= "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

conn.Open()

strSQL = "Select CustomerID , CompanyName from Customers"

cmd = New SqlCommand(strSql, conn)

dr = cmd.ExecuteReader()

Do While dr.Read
'ComboBox1.Items.Add(drCustomer.Item("CustomerID").ToString)
Trace.WriteLine(dr("CustomerID") & " - " & dr("CompanyName"))
Loop
conn.Close()


Ken
--------------------
[quoted text, click to view]
Hi Ken,

I tried that but I still see nothing...

Do I need to have it bound to a control on the form ?

Thanks,
Rob



[quoted text, click to view]


Rob
6/21/2005 12:00:00 AM
Thanks Terry, I made the change, but I did not see it do anything...


[quoted text, click to view]

Terry Olsen
6/21/2005 7:03:47 AM
Try using Debug.WriteLine instead.

[quoted text, click to view]

Rob
6/21/2005 8:22:23 AM
Hi Ken,

Thanks, that did get the data to show up in the Combo Box, however, the
Trace.Writeline does not appear to be working.

Also, how do you populate a form control from a Module ?

Thanks,
Rob


[quoted text, click to view]

AddThis Social Bookmark Button