weird combo box bind error

  • Thread starter Thread starter Ken Powers
  • Start date Start date
K

Ken Powers

Hello everyone,

I'm getting a strange error when I try to bind a combo box to a Dataset.
Here's my code!

Private Sub Get_Data()
Try
Dim sqlcmd = New SqlCommand("Select * from Utility order by
Utility", sqlConn)

daDataAdapter = New SqlDataAdapter
daDataAdapter.SelectCommand = sqlcmd

dsDataSet = New Dataset
daDataAdapter.Fill(dsDataSet, "Utility")


sqlcmd = New SqlCommand("Select * from Adjustments", sqlConn)
daDataAdapter = New SqlDataAdapter
daDataAdapter.SelectCommand = sqlcmd

daDataAdapter.Fill(dsDataSet, "Adjustments")

daRel = New DataRelation("UtilAdjust",
dsDataSet.Tables("Utility").Columns("Code"),
dsDataSet.Tables("Adjustments").Columns("Code"))
dsDataSet.Relations.Add(daRel)

Set_Update_Command()
Set_Insert_Command()
Set_Delete_Command()

Catch ex As Exception
MessageBox.Show(ex.Source.ToString & " - " & ex.Message,
Application.ProductName, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Private Sub BindFields()
Try
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
ctrl.DataBindings.Clear()
ctrl.Text = ""
End If
Next

cbUtility.DataBindings.Clear()
cbType.DataBindings.Clear()

For Each ctrl In Me.GroupBox1.Controls
If TypeOf ctrl Is TextBox Then
ctrl.DataBindings.Clear()
ctrl.Text = ""
End If
Next

cbUtility.DataSource = dsDataSet.Tables("Utility")
cbUtility.DisplayMember = "UTILITY"
cbUtility.ValueMember = "CODE"



tbAN.DataBindings.Add("Text", dsDataSet,
"Utility.UtilAdjust.AdjustmentName")
lblAK.DataBindings.Clear()
lblAK.DataBindings.Add("Text", dsDataSet,
"Utility.UtilAdjust.AdjKey")

BindDate(tbASD, "UTILITY.UtilAdjust.AdjStartDate")
BindDate(tbAED, "UTILITY.UtilAdjust.AdjEndDate")

BindCurr(tbAA, "UTILITY.UtilAdjust.adjustment")

BindType(cbType, "UTILITY.UtilAdjust.type")

bmUtil = Me.BindingContext(dsDataSet, "Utility")
bmAdjust = Me.BindingContext(dsDataSet, "UTILITY.UtilAdjust")

AddHandler Me.BindingContext(dsDataSet,
"Utility").PositionChanged, _
AddressOf dtUtil_PositionChanged

Catch ex As Exception
MessageBox.Show(ex.Source.ToString & " - " & ex.Message,
Application.ProductName, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub


First I get my data by calling Get_Data(), Next I bind my data be calling
BindFields()

I get the following error when I hit these statements. cbUtility = combo
box = System.Windows.Forms.ComboBox
cbUtility.DataSource = dsDataSet.Tables("Utility")
cbUtility.DisplayMember = "UTILITY"

Error:
"Microsoft.VisualBasic - Cast from type 'DataRowView' to type 'String' is
not valid"

Another odd thing is when the exception is raised my "Catch" doesn't pick it
up! I get an error message on those lines and the program keeps going?
Also everything works correctly, all controls are bound correctly, including
that combo box.

THanks,
 
Hi

Where is your table declaration

i.e. Dim ttable As New DataTable("Utility")

It may think "Utility" is a string without that
declaration.
-----Original Message-----
Hello everyone,

I'm getting a strange error when I try to bind a combo box to a Dataset.
Here's my code!

Private Sub Get_Data()
Try
Dim sqlcmd = New SqlCommand("Select * from Utility order by
Utility", sqlConn)

daDataAdapter = New SqlDataAdapter
daDataAdapter.SelectCommand = sqlcmd

dsDataSet = New Dataset
daDataAdapter.Fill(dsDataSet, "Utility")


sqlcmd = New SqlCommand("Select * from Adjustments", sqlConn)
daDataAdapter = New SqlDataAdapter
daDataAdapter.SelectCommand = sqlcmd

daDataAdapter.Fill(dsDataSet, "Adjustments")

daRel = New DataRelation("UtilAdjust",
dsDataSet.Tables("Utility").Columns("Code"),
dsDataSet.Tables("Adjustments").Columns("Code"))
dsDataSet.Relations.Add(daRel)

Set_Update_Command()
Set_Insert_Command()
Set_Delete_Command()

Catch ex As Exception
MessageBox.Show(ex.Source.ToString & " - " & ex.Message,
Application.ProductName, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Private Sub BindFields()
Try
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
ctrl.DataBindings.Clear()
ctrl.Text = ""
End If
Next

cbUtility.DataBindings.Clear()
cbType.DataBindings.Clear()

For Each ctrl In Me.GroupBox1.Controls
If TypeOf ctrl Is TextBox Then
ctrl.DataBindings.Clear()
ctrl.Text = ""
End If
Next

cbUtility.DataSource = dsDataSet.Tables ("Utility")
cbUtility.DisplayMember = "UTILITY"
cbUtility.ValueMember = "CODE"



tbAN.DataBindings.Add("Text", dsDataSet,
"Utility.UtilAdjust.AdjustmentName")
lblAK.DataBindings.Clear()
lblAK.DataBindings.Add("Text", dsDataSet,
"Utility.UtilAdjust.AdjKey")

BindDate
(tbASD, "UTILITY.UtilAdjust.AdjStartDate")
(tbAED, "UTILITY.UtilAdjust.AdjEndDate")
(tbAA, "UTILITY.UtilAdjust.adjustment")
BindType(cbType, "UTILITY.UtilAdjust.type")

bmUtil = Me.BindingContext (dsDataSet, "Utility")
bmAdjust = Me.BindingContext
(dsDataSet, "UTILITY.UtilAdjust")
 
Mike,

I'm just using the dataset to bind the combo to

Dim sqlcmd = New SqlCommand("Select * from Utility order by
Utility", sqlConn)

daDataAdapter = New SqlDataAdapter
daDataAdapter.SelectCommand = sqlcmd

dsDataSet = New Dataset
daDataAdapter.Fill(dsDataSet, "Utility")

cbUtility.DataSource = dsDataSet.Tables("Utility")
cbUtility.DisplayMember = "UTILITY"
cbUtility.ValueMember = "CODE"
 
Back
Top