|
If you are only ever going to retrieve the first column of the first row (or that the data only has one row, and the row only has one column) then you can use ExecuteScalar() - That will save you faffing around with DataReaders.
Here is an updated snippet:
myConnection.Open()<br />
Dim someValue As Object = myCommand.ExecuteScalar();<br />
myConnection.Close()
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
My website
|
|
|
|
|
That worked like a charm!
Thanks a lot for the help!
|
|
|
|
|
I answered your question in the sql forum, please don't cross post and please put more meaningful subject lines in your posts.
Ben
|
|
|
|
|
|
|
Hi, I got a problem that I could not solve when try to read my data from ms access database into the list box. I have the following code:
Under the form public class:
Private WithEvents cnnDonorDb As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\DICDonorDb\DICDornorDb.mdb'; Persist Security Info=False")
I add the following code to form load event to get data from the database to the list box:
Dim cmdOrgType As New OleDbCommand
With cmdOrgType
.Connection = cnnDonorDb
.CommandType = CommandType.Text
.CommandText = "SELECT * FROM tblOrgType.OrgType ORDER BY tblOrgType.OrgType;"
End With
' Open the connection.
cmdOrgType.Connection.Open()
' Execute the command to the return the list of organization type.
cmdOrgType.ExecuteReader() ' HERE IS THE PLACE THAT I GET AN ERROR
Dim drdResult As OleDbDataReader
' Get the list of organization type and put it in the list box.
While drdResult.Read
For colOrgType As Integer = 0 To drdResult.FieldCount - 1
lsbOrgInfo.Items.Add(drdResult(colOrgType).ToString)
Next
End While
' Close the result.
drdResult.Close()
' Close the connection
cmdOrgType.Connection.Close()
I got an error as the following message:
Could not find file 'C:\Documents and Settings\roathkanel\My Documents\Visual Studio 2005\Projects\DIC Donor Database\DIC Donor Database\bin\Debug\tblOrgType.mdb'.
I really don't understand why the database path in this message is difference from database path in C: drive. As you can see the path in the connection string is C:\DICDonorDb\DICDornorDb.mdb . I'm sure that the file is located in this folder because I didn't type it, i copy the string path and file name into my code editor.
Please help me to solve this problem. I'm using VB 2005.
Thank in advance!!!
|
|
|
|
|
There's a few problems with this...
First, this:
Private WithEvents cnnDonorDb As New System.Data.OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\DICDonorDb\DICDornorDb.mdb';" & _
"Persist Security Info=False")
doesn't need the With Events clause. Well, no unless you're going to handle any of the three events that it exposes. Just about noone does...
Second, in your SQL statement, you specified the name of the database in your table spec. Actually, you treated the name of the table as a database name:
SELECT * FROM tblOrgType.OrgType ORDER BY tblOrgType.OrgType;
assuming that tblOrgType is a table and OrgType is a column in that table:
SELECT OrgType FROM tblOrgType ORDER BY OrgType
Altogether, your code should read more like this: Do NOT hold open a database connection object for the life of your application.
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\DICDonorDb\DICDornorDb.mdb""; Persist Security Info=False;"
Dim conn As New OldDbConnection(connString)
Dim cmdOrgType As New OleDbCommand
With cmdOrgType
.Connection = cnnDonorDb
.CommandType = CommandType.Text
.CommandText = "SELECT OrgType FROM tblOrgType ORDER BY OrgType"
End With
' Open the connection.
cmdOrgType.Connection.Open()
' Execute the command to the return the list of organization type.
Dim drdResult As OleDbDataReader
drdResult = cmdOrgType.ExecuteReader()
' Get the list of organization type and put it in the list box.
While drdResult.Read
For colOrgType As Integer = 0 To drdResult.FieldCount - 1
lsbOrgInfo.Items.Add(drdResult(colOrgType).ToString)
Next
End While
' Close the result.
drdResult.Close()
' Close the connection
cmdOrgType.Connection.Close()
This, of course, does NOT mean this is good code. There's no exception handling in your code and it makes a bunch of assumptions where it's running in a "perfect world" where everything work the way it should.
|
|
|
|
|
Hi, thank you for your comment and the previous problem are solved based on your code. But another problem is occure in the above code:
...
cmdOrgType.Connection.Open()
Dim drdResult As OleDbDataReader
drdResult = cmdOrgType.ExecuteReader()
While drdResult.Read ' Here is the problem occure.
For colOrgType As Integer = 0 To drdResult.FieldCount - 1 lsbOrgInfo.Items.Add(drdResult(colOrgType).ToString)
Next
End While
drdResult.Close()
cmdOrgType.Connection.Close()
Here is an error message:
Variable 'drdResult' is used before it has been assigned a value. A null reference exception could result at runtime
I think that the reason of cause the problem because my table do not have any record so the data reader object could not read the value. If it is possible, could you please let me know the sample code to check whether there is an existing record in the table?
Thank in advance!!!
|
|
|
|
|
You can check to see if a DataReader was actually returned with:
drdResult = cmdOrgType.ExecuteReader()
If Not drdResult Is Nothing Then
While ...
|
|
|
|
|
Thank you very much for your help. I will try this code.
|
|
|
|
|
Hi
I am vb.net developer i want to use aop (Aspect Oriented Programming) in visual basic .net
How to Use it ??
Thank you
Worachat
|
|
|
|
|
|
Please let me know the name of constant identifier for executing applications path in Visual basic 6.0
|
|
|
|
|
Try
App.Path
Develop2Program & Program2Develop
|
|
|
|
|
|
hi
I have some forms and these forms are actually related. what i want to do is that in form2 ,i want to access and modify the contents of the text boxes in form1, while i am still in form2. how can i do this ? do i have to declare them all as Public shared in from1.
Thank You
Kokemo
|-|■~F~|-|■
|
|
|
|
|
KOKEMO wrote: how can i do this ?
See Passing Values between Forms[^]
KOKEMO wrote: do i have to declare them all as Public shared in from1.
That would be unwise.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
My website
|
|
|
|
|
Hi,
In form2
Private m_f As Form1
Public Sub New(ByVal f As Form1)
Call Me.New
m_f = f
End Sub
Public Sub New()
InitializeComponent()
End Sub
...
Now, you can use m_f to refer to form1
in Form1
Dim f As New Form2(Me)
f.Show
regards
|
|
|
|
|
This was brought up yesterday.
You do NOT modify the controls on Form1 from Form2. Form2 shouldn't know anything about Form1, nor should it care. All it should care about is the data that it's supposed to manipulate.
If you're showing Form2 using ShowDialog() , then Form1 creates an instance of Form2 and waits for Form2 to be dismissed. Form2 exposes any data that it should as public properties or as some data object. Form1, when it gets control back, looks at the data in Form2 properties or in the data object that it modified and then modifies its own controls accordingly.
|
|
|
|
|
Dave Kreskowiak wrote: do NOT modify the controls on Form1 from Form2
Seems like there has been a rash of these types of questions lately
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Makes me think there's a new school year somewhere...
|
|
|
|
|
Ohhhhhh joy
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
i searched alot about how to print datagrid and all i get was alot of code and classes .
i have a populated datagrid
all i want is to print the datagrid as its , i don't want to manipulate the cells size or fonts or any thing ..... just print it completly
note : i had tried to print it but the printed part was the monitor showing part ... i need to print it completly
and thanks for all
|
|
|
|
|
Mr.Kode wrote: i searched alot about how to print datagrid and all i get was alot of code and classes .
What did you expect you were going to get?? Something that does it for you automatically??
This article[^] is about as generic as you're going to get.
|
|
|
|
|