1,105,290 Community Members

Populating Menu strip drop down with database result

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

Hello Guys, Here is an interesting problem.
I have the following code. But it seems like I am missing out something in the process of populating drop down menus with data from database.

You guys have a look at it and please give your valuable suggestions.

Private Sub Open_Exam()
        Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\VB Applications\PROJECT_1\PROJECT_1\examdb.mdf;Integrated Security=True;User Instance=True")
        Dim cmd As New SqlCommand("Select Exam_No from Exam_Master Where Author='" + mniUsername.Text + "'", con)
        con.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()

        While dr.Read()
            tsmOpen_Exam.DropDown.Items.Add(dr.GetString)
        End While

        dr.Close()
        con.Close()
    End Sub

Thanks.

Member Avatar
Philippe.Lahaie
Posting Whiz
376 posts since Oct 2007
Reputation Points: 42 [?]
Q&As Helped to Solve: 60 [?]
Skill Endorsements: 9 [?]
 
0
 

Are you getting errors or is the result you are getting simply not the result you are expecting?

What are the error(s) or what is the result you get/expect?

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

@Philippe: I am getting error as follows:

Error 1 Overload resolution failed because no accessible 'GetString' accepts this number of arguments. C:\VB Applications\PROJECT_1\PROJECT_1\Welcome.vb 80 45 PROJECT_1

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

As the error is ellliminated now by using dr.ToString()
but the data is not fetched .....am getting unexpected result like.

System.Data.SqlClient.SqlDataReader

Trying to figure out where i Went Wrong.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

Are you using the Visual Basic source code editor to enter your code?
If so, try paying attention to the IntelliSence prompts. intell1
It informs you that you need to specify the column to retrieve for the GetString method.

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

@TnTinMN: If you pay attention to code that I posted, I have only requested one column.
So how should i Specify it ??

Member Avatar
Philippe.Lahaie
Posting Whiz
376 posts since Oct 2007
Reputation Points: 42 [?]
Q&As Helped to Solve: 60 [?]
Skill Endorsements: 9 [?]
 
0
 

the GetString method expects an integer value as argument that represent the 0-based index of the table columns.

In your case, the only column from your select is Exam_No and this value can be fetched with the index 0.

Note that since the value of Exam_No is probably an integer, it might require you to do .GetInt32(0).ToString() or even just .GetValue(0).ToString()

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article