AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Syed Adeel Rizvi
Expertise
I can answers questions regarding web based and desktop based programming in VB.Net. Which can include XML, Custom Controls + Computer Hardware and Windows Turboshooting...etc. More Skills : 1 . PHP - > All Type Of CMS e.g. (Joomla,Drupul,PHPNuke...etc) 2 . ASP 3 . ASP.Net 4 . CMS 5 . MySql 6 . SQl Server 2000 & 2005 7 . Ms Access 8 . Web Designing 9 . Networking Turboshooting 10 . Windows Turboshooting 11 . Hardware Turboshooting All Type Of Work Related To IT I also Do Work as a Freelance as Application & Web Developer & Designer

Experience
i have 3 years work experience in software house and 4 years work experience for computer hardware and Networking..

Organizations
110 Solutions

Education/Credentials
Bsc (Honors)

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Select item from a drop down list

Topic: VB.NET



Expert: Syed Adeel Rizvi
Date: 7/19/2007
Subject: Select item from a drop down list

Question
Hi,

I am writing a program in vs2005 using vb.net. My problem lies in the area where I have two drop down lists called "cadre" and "grade". The grade populates depending on the value of cadre. All the values come from the database. A table in the database has columns cadre and grade and cadre repeats for all different values of grade. For instance, if I have 4 grades for cadre 1, then in the cadre column cadre 1 will repeat 4 times, each having a unique grade value.

To populate the grade ddl, I initially tried to do this:

Dim cmd12 As New SqlCommand("SELECT Cgrade from access_control.dbo.table_cadre" & _
" WHERE cadre = '" & ddl_cadre.SelectedItem.Text & "' ", consql)

cmd12.Connection.Open()
reader = cmd12.ExecuteReader()
ddl_cgrade.DataSource = reader
ddl_cgrade.DataTextField = "Cgrade"
ddl_cgrade.DataBind()
reader.Close()
cmd12.Connection.Close()

This generated an error "Object reference not set to an instance of an object." So I tried to figure out ddl_cadre.SelectedItem.Text actually displays a value. I said

txt_box.text = ddl_cadre.SelectedItem.Text

I first put this code in SelectedIndexchanged function and it didn't do anything. Then I placed it in a button and the text would only be the very first value in the cadre list (which is "SELECT" and it isnt really a value).

To make sure that the problem is not with SELECT not being a choice in the cadre database, I added SELECT and some random value for grade by it. I still got the same error.

So, I am not sure exactly where the real problem lies but it looks like ddl_cadre.SelectedItem.Text doesn't give the right value. Moreover, just by choosing an option in the cadre box, the text doesn't appear in a text box, I have to hit a button for it.

In short, I want to be able to select the right choice from the cadre box and also automatically populate the grade box based on the selection. I appreciate your assistance.

Thanks,
Fauzia

Answer
Hello

 So i simple words you want to pass the value from ddl to SQL query hmm ok...

here :

Your code...

Dim cmd12 As New SqlCommand("SELECT Cgrade from access_control.dbo.table_cadre" & _
" WHERE cadre = '" & ddl_cadre.SelectedItem.Text & "' ", consql)

cmd12.Connection.Open()
reader = cmd12.ExecuteReader()
ddl_cgrade.DataSource = reader
ddl_cgrade.DataTextField = "Cgrade"
ddl_cgrade.DataBind()
reader.Close()
cmd12.Connection.Close()

follow my code :
Dim CMD as SQLCommand

Cmd.CommandText = "Select Cgrade from access_control.dbo.table_cadre" & _
"Where cadre='" & ddl_cadre.SelectedValue.ToString & "'", consql

cmd.Connection.Open()
reader = cmd.ExecuteReader()
ddl_cgrade.DataSource = reader
ddl_cgrade.DataTextField = "Cgrade"
ddl_cgrade.DataBind()
reader.Close()
cmd.Connection.Close()

if still problem then plz mail me ur project at syedadeel2@gmail.com thank you  

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.