Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Drop-Down menus

Status
Not open for further replies.
Mar 14, 2002
711
US
I am trying to populate a drop-down based on a query, I have text boxes which I update using the DataFill method, so being the rookie I am, I thought I could do the same for drop downs, but not so...

Const StrConnString As String = "server......="

Dim objConn As New SqlConnection(StrConnString)
Dim cmdSelect2

cmdSelect2 = New SqlCommand("Select * From Kaizen where KaizenID = '" & strID & "' ", objConn)

Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmdSelect2)

Dim myDataSet = New DataSet()

myDA.Fill(myDataSet, "Kaizen")

Dim Row As DataRow

If myDataSet.Tables("Kaizen").Rows.Count > 0 Then

Row = myDataSet.Tables("Kaizen").Rows(0)

End If

If Row.Item("LongDescription") Is DBNull.Value Then
DescrTxt.Text = String.Empty
Else
DescrTxt.Text = Row.Item("LongDescription")

End If

If Row.Item("Status") Is DBNull.Value Then
Status.SelectedItem.Value = String.Empty
Else
Status.SelectedItem.Value = Row.Item("Status")

End If

My code fails on the Status...it does not like that at all, any ideas??

Thanks!
 
hi,

what exactly is the error stating???

Known is handfull, Unknown is worldfull
 
Object reference not set to an instance of an object

Kaizen.SupervisorForm.Page_Load(Object sender, EventArgs e) in C:\Inetpub\
This is where it fails:

If Row.Item("Status") Is DBNull.Value Then
Status.SelectedItem.Value = String.Empty
Else
Status.SelectedItem.Value = Row.Item("Status")

End If
 
You don't say exactly which line causes the error but I assume it is one of these two:
Code:
Status.SelectedItem.Value = String.Empty
Code:
Status.SelectedItem.Value = Row.Item("Status")
If it is, it probably means that either the Status control can't be found or the Row object can't be found. If it's the former it could be that VS hasn't created the reference to it (which sometimes happened in VS2003). Try removing and then re-adding the control if this is the case and/or show us your code to create this control.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It seems to be the Dropdown list, when I send the field to a text box, it works without any issues?
 
In that case, it's either the problem I mentioned above (which you should check even if it's just to eliminate it) or it's the way you are setting the SelectedValue. To select an Item (which is different to setting the Value of the control), I usually use the FindByValue method and once I've returned an Item, I set it's Selected property to True.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
>> Status.SelectedItem.Value

is any item selected in the dropdown at this time?

try this:
if not Status.SelectedItem is nothing then
If Row.Item("Status") Is DBNull.Value Then
Status.SelectedItem.Value = String.Empty
Else
Status.SelectedItem.Value = Row.Item("Status")

End If
end if

Known is handfull, Unknown is worldfull
 
try this:
if not Status.SelectedItem is nothing then
If Row.Item("Status") Is DBNull.Value Then
Status.SelectedItem.Value = String.Empty
Else
Status.SelectedItem.Value = Row.Item("Status")

End If
end if
Remember, that will only change the Value not select the Item like I mentioned above. It would be more appropiate to select the Item itself i.e.
Code:
myDropDownList.Items.FindByValue("100").Selected = True


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
yup,

i was only trying to point out where the NullReference exception could arise from.

the code inside was just a copy paste...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top