Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection
Bound Combobox Selection Bound Combobox Selection
Bound Combobox Selection
Go Back  Xtreme Visual Basic Talk > > > Bound Combobox Selection


Reply
 
Thread Tools Display Modes
  #1  
Old 07-08-2004, 02:25 PM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default Bound textboxes formatting problem


I keep getting a specified cast is not valid error. I am pulling all my data from an access database and used the dataform wizard. Below is the snippet where I keep bombing out. Any help would truly be appreciated. Thanks.

Quote:
Private Sub frm_bats_closeout_tracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.editInformal_Notice_Sent.Text = Format(objCloseout1.Revised_Contract_Table.Informal_Notice_SentColumn, "mm/dd/yyyy")

Try
'Attempt to load the dataset.
Me.LoadDataSet()
Catch eLoad As System.Exception
'Add your error handling code here.
'Display error message, if any.
System.Windows.Forms.MessageBox.Show(eLoad.Message)
End Try
Me.objCloseout1_PositionChanged()
End Sub
Reply With Quote
  #2  
Old 07-08-2004, 02:49 PM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, this line:

Me.editInformal_Notice_Sent.Text = Format(objCloseout1.Revised_Contract_Table.Informal_Notice_SentColumn, "mm/dd/yyyy")

What is the defined data type of the Informal_Notice_SentColumn, and where is this coming from...what type of data binding is this? You can't simply assign a text box a database value in this manner.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 07-09-2004, 09:52 AM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

Alright I found something online that helped but do you happen to have any ideas on how to loop this or something of that nature. I would hate to have to call a sub routine for each textbox.

Quote:
Dim WithEvents oBinding As Binding

Private Sub SetControlBindings()
oBinding = New Binding("Text", MyDataset, "Revised_Contract_Table.Informal_Notice_Sent")
TextBox1.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Prog_Contract_#")
TextBox2.DataBindings.Add(oBinding)
TextBox3.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.1st_Formal_Notice_Sent")
TextBox4.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.2nd_Formal_Notice_Sent")
TextBox5.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_CO_Received")
TextBox6.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Assigned")
TextBox7.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Staff_Name")
TextBox8.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Verification_Complete")
TextBox9.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Accepted_By_TWc")
TextBox10.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Mailed_To_Contractor")
TextBox11.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Recapture_Amt")
TextBox12.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Admin_CloseOut")
End Sub

Private Sub oBinding_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles oBinding.Format
e.Value = Format(e.Value, "MM/dd/yy")
End Sub
Reply With Quote
  #4  
Old 07-09-2004, 10:53 AM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Basically, create a control array of textboxes. Then you loop until the upper bound of the control array and assign the databindings to each control.

If you are not familiar with control arrays in .Net, here is a nice tutorial on the topic:

http://www.xtremevbtalk.com/tutors-corner/174835-control-arrays-net.html
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 07-09-2004, 12:38 PM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

I have gone over the tutorial and I'm still not too sure I understand. Is there anyway that you may be able to provide a snippet of code?

I guess I'm not sure how I do this with the binding.

Quote:
Originally Posted by MKoslof
Basically, create a control array of textboxes. Then you loop until the upper bound of the control array and assign the databindings to each control.

If you are not familiar with control arrays in .Net, here is a nice tutorial on the topic:

Control Arrays in .NET
Reply With Quote
  #6  
Old 07-09-2004, 06:26 PM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

http://msdn.microsoft.com/library/de...VisualCNET.asp

This is a more comprehensive example..it show you how to do this, step by step. Once your control array is finished, you can assign the databindings you want at each index (or for each item within it).

If you don't want to use a control array, each text box will have to be coded one at a time, no other way around it
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #7  
Old 07-12-2004, 08:19 AM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

First and foremost, thanks for all the help so far.

Secondly I have added what I have come up with. It could be completely wrong (and I'm pretty sure it is since it isn't working). Can you see what I am doing wrong?


Quote:
Friend obinding(12) As Binding
Private Sub SetControlBindings()

Dim obinding2 As New Binding("Text", MyDataset, "Revised_Contract_Table.Informal_Notice_Sent")
Dim obinding3 As New Binding("Text", MyDataset, "Revised_Contract_Table.1st_Formal_Notice_Sent")
Dim obinding4 As New Binding("Text", MyDataset, "Revised_Contract_Table.2nd_Formal_Notice_Sent")
Dim obinding5 As New Binding("Text", MyDataset, "Revised_Contract_Table.Date_CO_Received")
Dim obinding6 As New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Assigned")
Dim obinding8 As New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Verification_Complete")
Dim obinding9 As New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Accepted_By_TWc")
Dim obinding10 As New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Mailed_To_Contractor")


Dim lv As Integer
For lv = 0 To 12
obinding(lv) = Binding

AddHandler obinding(lv).Format, AddressOf oBinding_Format
Next

TextBox2.DataBindings.Add(obinding2)
TextBox3.DataBindings.Add(obinding3)
TextBox4.DataBindings.Add(obinding4)
TextBox5.DataBindings.Add(obinding5)
TextBox6.DataBindings.Add(obinding6)
TextBox7.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Staff_Name")
TextBox8.DataBindings.Add(obinding8)
TextBox9.DataBindings.Add(obinding9)
TextBox10.DataBindings.Add(obinding10)
TextBox11.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Recapture_Amt")
TextBox12.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Admin_CloseOut")
End Sub

Private Sub oBinding_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs)
e.Value = Format(e.Value, "MM/dd/yy")
End Sub
Reply With Quote
  #8  
Old 07-12-2004, 08:54 AM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default Bound Combobox Selection

I'm having a problem where I have a combobox as well as various textboxes. The textboxes are supposed to populate based on what is changed in the combobox. It works fine the first time but then I get an error if I try to change the combobox selection. It tells me that "it would cause two bindings in the collection to bind to the same property". I've tried clearing the bindings but then the combobox selection changes but nothing else does. ANY IDEAS?

Quote:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
MyTable = "Revised_Contract_Table"
Dim Connect As New OleDb.OleDbConnection(strconnect)
Dim str1 As String = ComboBox1.SelectedItem
Connect.Open()
sql = "SELECT * FROM Revised_Contract_Table WHERE [Prog_Contract_#] = ('" & str1 & "')"
Dim MyAdapter As New OleDb.OleDbDataAdapter(sql, Connect)
Try
RecordsRead = MyAdapter.Fill(MyDataset, MyTable)
SetControlBindings()
Connect.Close()
MyAdapter = Nothing
Connect = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Reply With Quote
  #9  
Old 07-12-2004, 09:20 AM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

So how have you set up your databindings? Basically where is this query run, so this query is supposed to by dynamic, with "str1" changing to the requested SelectedItem. And if you run your code in debug mode, after the FIRST time..what is the value of str1. If you run the code again..what is the value of str1..is this value changing dynamically?

And what is in "SetControlBindings"?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #10  
Old 07-12-2004, 09:44 AM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

No I still haven't been able to set up the databindings.

I had actually posted the combobox problem as a seperate thread but I guess they were joined by the moderator.

You are correct in that the query is supposed to be dynamic with each change in the combobox (which should equal str1). I set up str1 to a textbox (to test it) and it appears that it is in fact changing with each change to the combobox.

SetControlBindings consists of the previous post I was having issues with.
Quote:
Private Sub SetControlBindings()
oBinding = New Binding("Text", MyDataset, "Revised_Contract_Table.Informal_Notice_Sent")
TextBox1.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Prog_Contract_#")
TextBox2.DataBindings.Add(oBinding)
TextBox3.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.1st_Formal_Notice_Sent")
TextBox4.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.2nd_Formal_Notice_Sent")
TextBox5.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_CO_Received")
TextBox6.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Assigned")
TextBox7.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Staff_Name")
TextBox8.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Verification_Complete")
TextBox9.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Accepted_By_TWc")
TextBox10.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Date_Mailed_To_Contractor")
TextBox11.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Recapture_Amt")
TextBox12.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Comments")
TextBox13.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Vendor_Name")
TextBox14.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Begin_Date")
TextBox15.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.End_Date")
TextBox16.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Contract_Amt")
editAdmin_CloseOut.DataBindings.Add("Checked", MyDataset, "Revised_Contract_Table.Admin_CloseOut")
End Sub
Private Sub oBinding_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles oBinding.Format
e.Value = Format(e.Value, "MM/dd/yy")
End Sub
Reply With Quote
  #11  
Old 07-12-2004, 09:48 AM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Yes, I merged both threads. OK, there are two places where you need to look. There are two kinds of databinds. Simple and complex. The MSDN provides a great overview of all this. Look here in your help file:

ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconConsumersOfDataOnWindowsForms.htm


And really, I think a better method for you here might be to enurate all controls on your form, and then set the needed databindings. Why your code isn't working is because currently the databindings are not set up correctly. Go to this website and download this code example:

http://www.mvps.org/dotnet/dotnet/sa...teControls.zip

You can run their sample and tweak the code in your own solution file. This will enumerate all controls on the given form, then you can assign the databindings.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #12  
Old 07-12-2004, 02:27 PM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

Alright - I got the bindings to work just fine, however, I'm still having an issue with the combobox. As I mentioned earlier, the first time i make a selection the other textboxes populate just fine. However nothing changes when I change the selection in the combobox. Anybody have any ideas as to what the problem could be?


The combobox is populated as follows:

Code:
Private Sub frm_bats_closeout_tracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cnn1 As New OleDb.OleDbConnection(strconnect)
        Dim cm As New OleDb.OleDbCommand("SELECT * FROM Revised_Contract_Table", cnn1)
        Dim dr As OleDb.OleDbDataReader
        Try
            cnn1.Open()
            dr = cm.ExecuteReader(CommandBehavior.CloseConnection)
            While dr.Read()
                ComboBox1.Items.Add(dr("Prog_Contract_#"))
            End While

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            cm.Dispose()
            cm = Nothing
            cnn1.Dispose()
            cnn1 = Nothing
            dr = Nothing
        End Try
    End Sub
This is what runs when a selection is made on the combobox:

Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        MyTable = "Revised_Contract_Table"
        Dim Connect As New OleDb.OleDbConnection(strconnect)
        Dim str1 As String = ComboBox1.SelectedItem
        Connect.Open()
        sql = "SELECT * FROM Revised_Contract_Table WHERE [Prog_Contract_#] = ('" & str1 & "')"
        Dim MyAdapter As New OleDb.OleDbDataAdapter(sql, Connect)
        Try
            RecordsRead = MyAdapter.Fill(MyDataset, MyTable)
            ClearControlBindings()
            SetControlBindings()
            Connect.Close()
            MyAdapter = Nothing
            Connect = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
These are the setcontrolbindings and clearcontrolbindings I'm using:
Code:
Private Sub SetControlBindings()
        TextBox1.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Prog_Contract_#")
        Dim b2 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Informal_Notice_Sent")
        Dim b3 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.1st_Formal_Notice_Sent")
        Dim b4 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.2nd_Formal_Notice_Sent")
        Dim b5 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Date_CO_Received")
        Dim b6 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Assigned")
        TextBox7.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Staff_Name")
        Dim b8 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Verification_Complete")
        Dim b9 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Accepted_By_TWc")
        Dim b10 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Date_Mailed_To_Contractor")
        Dim b11 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Recapture_Amt")
        TextBox12.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Comments")
        TextBox13.DataBindings.Add("Text", MyDataset, "Revised_Contract_Table.Vendor_Name")
        Dim b14 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Begin_Date")
        Dim b15 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.End_Date")
        Dim b16 As Binding = New Binding("Text", MyDataset, "Revised_Contract_Table.Contract_Amt")
        editAdmin_CloseOut.DataBindings.Add("Checked", MyDataset, "Revised_Contract_Table.Admin_CloseOut")

        AddHandler b2.Format, AddressOf LongtoShort
        AddHandler b2.Parse, AddressOf ShorttoLong
        AddHandler b3.Format, AddressOf LongtoShort
        AddHandler b3.Parse, AddressOf ShorttoLong
        AddHandler b4.Format, AddressOf LongtoShort
        AddHandler b4.Parse, AddressOf ShorttoLong
        AddHandler b5.Format, AddressOf LongtoShort
        AddHandler b5.Parse, AddressOf ShorttoLong
        AddHandler b6.Format, AddressOf LongtoShort
        AddHandler b6.Parse, AddressOf ShorttoLong
        AddHandler b8.Format, AddressOf LongtoShort
        AddHandler b8.Parse, AddressOf ShorttoLong
        AddHandler b9.Format, AddressOf LongtoShort
        AddHandler b9.Parse, AddressOf ShorttoLong
        AddHandler b10.Format, AddressOf LongtoShort
        AddHandler b10.Parse, AddressOf ShorttoLong
        AddHandler b11.Format, AddressOf DecimalToCurrencyString
        AddHandler b11.Parse, AddressOf CurrencyStringToDecimal
        AddHandler b14.Format, AddressOf LongtoShort
        AddHandler b14.Parse, AddressOf ShorttoLong
        AddHandler b15.Format, AddressOf LongtoShort
        AddHandler b15.Parse, AddressOf ShorttoLong
        AddHandler b16.Format, AddressOf DecimalToCurrencyString
        AddHandler b16.Parse, AddressOf CurrencyStringToDecimal
        TextBox2.DataBindings.Add(b2)
        TextBox3.DataBindings.Add(b3)
        TextBox4.DataBindings.Add(b4)
        TextBox5.DataBindings.Add(b5)
        TextBox6.DataBindings.Add(b6)
        TextBox8.DataBindings.Add(b8)
        TextBox9.DataBindings.Add(b9)
        TextBox10.DataBindings.Add(b10)
        TextBox11.DataBindings.Add(b11)
        TextBox14.DataBindings.Add(b14)
        TextBox15.DataBindings.Add(b15)
        TextBox16.DataBindings.Add(b16)
    End Sub

    Public Sub ClearControlBindings()
        TextBox1.DataBindings.Clear()
        TextBox2.DataBindings.Clear()
        TextBox3.DataBindings.Clear()
        TextBox4.DataBindings.Clear()
        TextBox5.DataBindings.Clear()
        TextBox6.DataBindings.Clear()
        TextBox7.DataBindings.Clear()
        TextBox8.DataBindings.Clear()
        TextBox9.DataBindings.Clear()
        TextBox10.DataBindings.Clear()
        TextBox11.DataBindings.Clear()
        TextBox12.DataBindings.Clear()
        TextBox13.DataBindings.Clear()
        TextBox14.DataBindings.Clear()
        TextBox15.DataBindings.Clear()
        TextBox16.DataBindings.Clear()
        editAdmin_CloseOut.DataBindings.Clear()
    End Sub
Reply With Quote
  #13  
Old 07-13-2004, 06:42 AM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, well this is the problem that I see. You have text boxes that are bound to a dataset via databindings. But your combo box is NOT a bound control. The combo box is being populated via the Items.Add method. If you want to create a bound combo box you would have to create a public class and bind the Display and Value members to the same datasource as the text boxes. So, when you change a value in the combo box the text boxes will not change, because the combo box is not bound to the same datasource.

I would suggest either binding your combo box to the same databindings as your text boxes, or unbind everything . Be consistent either way. You could simply code the change event of the combo box and assign the .text property of each text box to a row within the returned DataSet. This would be the unbound option. So, create a class or function, that runs whenever a new value is selected in the combo box. It will run a new sql statement, clear the old dataset and send the text boxs the returned values. Or again, bind the combo box so it correlates with the databindings of the text boxes..those are the two main options you have.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #14  
Old 07-13-2004, 09:04 AM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

Alright, took your advice and made everything unbound. I simply used a datatable and datarow filter to try and accomplish my goal. However I am still getting an error at this line:

TextBox1.Text = dr("Prog_Contract_#")

It says "object reference not set to an instance of the object". Any ideas?

Code:
Private Sub frm_bats_closeout_tracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyTable = "Revised_Contract_Table"
        Dim Connect As New OleDb.OleDbConnection(strconnect)
        Dim str1 As String = ComboBox1.SelectedItem
        Connect.Open()
        sql = "SELECT * FROM Revised_Contract_Table"
        Dim MyAdapter As New OleDb.OleDbDataAdapter(sql, Connect)
        Try
            RecordsRead = MyAdapter.Fill(MyDataset, MyTable)
            Connect.Close()
            MyAdapter = Nothing
            Connect = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Dim dt As New DataTable
        Dim dr As DataRow
        dt = MyDataset.Tables(0)
        Dim pk As DataColumn = dt.Columns("Prog_Contract_#")
        dt.PrimaryKey = New DataColumn() {pk}
        For Each dr In dt.Rows()
            ComboBox1.Items.Add(dr("Prog_Contract_#"))
        Next
        ComboBox1.SelectedIndex = -1
    End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim dr As DataRow
        dr = MyDataset.Tables("Revised_Contract_Table").Rows.Find(ComboBox1.SelectedValue)
        TextBox1.Text = dr("Prog_Contract_#")
    End Sub
Reply With Quote
  #15  
Old 07-13-2004, 09:24 AM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Try passing in the "MyDataSet" dataset object byVal, so the object in question is fully instantiated. So, create a sub that you call with the selectedIndexChanged event:

Code:
Public Sub assignVal(ByVal D as DataSet) Dim dr as DataRow dr = d.Tables(0).Rows.Find(ComboBox1.SelectedValue) Textbox1.Text = dr("Prog_Contact_#").ToString End Sub

And if you step through the code, make sure you are getting the proper value and not null or nothing. You could always trap this, something like:

Code:
'after assigning it to d.Tables(0).... If Not dr Is Nothing Then TextBox1.Text = ... End If
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #16  
Old 07-13-2004, 10:52 AM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

I'm still getting the same error. Any other ideas on what I could be doing wrong?
Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        assignVal(MyDataset)
    End Sub

    Public Sub assignVal(ByVal D As DataSet)
        Dim dr As DataRow

        dr = d.Tables(0).Rows.Find(ComboBox1.SelectedValue)
        Textbox1.Text = dr("Prog_Contact_#").ToString
    End Sub
Reply With Quote
  #17  
Old 07-13-2004, 12:07 PM
MKoslof's Avatar
MKoslofBound Combobox Selection MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, make sure that you are physically passing in the right DataSet. Does this work?

Code:
Public Sub Test(ByVal d as DataSet) Dim dr as DataRow For each dr in d.Tables(0).rows msgbox dr(0) msgbox dr(1) Next End Sub

Does this return valid data? If so, maybe the "Prog_Contact_#" is not a recognized field. Try dr(0).ToString. If this doesn't show anything or throws an error, show me how you define "MyDataSet". Before passing it as a value to your sub where do you create the actual instance of MyDataSet and populate it?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #18  
Old 07-13-2004, 12:16 PM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

I tried dr(0).ToString and I still get the same error.

Code:
Private RecordsRead As Long
    Private MyDataset As New DataSet
    Dim strconnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\Documents and Settings\mondror1\My Documents\Monitoring\Secure Close_Out_Tracking.mdb"
    Private MyTable As String

Public Sub frm_bats_closeout_tracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyTable = "Revised_Contract_Table"
        Dim Connect As New OleDb.OleDbConnection(strconnect)
        Connect.Open()
        sql = "SELECT * FROM Revised_Contract_Table"
        Dim MyAdapter As New OleDb.OleDbDataAdapter(sql, Connect)
        Try
            RecordsRead = MyAdapter.Fill(MyDataset, MyTable)
            Connect.Close()
            MyAdapter = Nothing
            Connect = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        Dim dt As New DataTable
        Dim dr As DataRow
        dt = MyDataset.Tables(0)
        Dim pk As DataColumn = dt.Columns("Prog_Contract_#")
        dt.PrimaryKey = New DataColumn() {pk}
        For Each dr In dt.Rows()
            ComboBox1.Items.Add(dr("Prog_Contract_#"))
        Next
        ComboBox1.SelectedIndex = -1
    End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        assignVal(MyDataset)
    End Sub

    Public Sub assignVal(ByVal D As DataSet)
        Dim dr As DataRow

        dr = D.Tables(0).Rows.Find(ComboBox1.SelectedValue)
        TextBox1.Text = dr("Prog_Contract_#").ToString
    End Sub
Reply With Quote
  #19  
Old 07-13-2004, 12:39 PM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

Nevermind, I got it...simple slip of the brain. It should be selecteditem as opposed to selected value. Thanks for the help.
Reply With Quote
  #20  
Old 07-13-2004, 01:32 PM
oralb5 oralb5 is offline
Regular
 
Join Date: Jun 2004
Posts: 82
Default

Alright, one more question...now I am getting the following error:

Quote:
Cast from type 'DBNull' to type 'String' is not valid.
Now I know it is because I have null values coming in from the database. Can you think of any way to iterate through the various fields in a selected row in order to avoid this error?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection Bound Combobox Selection
Bound Combobox Selection
Bound Combobox Selection
 
Bound Combobox Selection
Bound Combobox Selection
 
-->