DataBinding

G

Guest

H

I am running a stored procedure on the SelectedIndexChanged event of a combobox. this stored procedure retrieves details from a database. i want to bind the results to the controls on my form but it doesnt work!! here is my cod

'Display the caravan details based on the length and the caravan model
Dim cmdEditCara As New SqlCommand("SPSelCaraDetailsOnModelLength", Conn
Dim da As New SqlDataAdapter(cmdEditCara
Dim ds As New DataSet(

'run a command to fill the textboxes et
cmdEditCara.CommandType = CommandType.StoredProcedur

'declare the parameter
cmdEditCara.Parameters.Add("@CaraModel", SqlDbType.Char, 50
cmdEditCara.Parameters.Add("@Length", SqlDbType.Char, 10
'now set the value
cmdEditCara.Parameters("@CaraModel").Value = CBoxEditCaraName.Tex
cmdEditCara.Parameters("@Length").Value = CBoxEditCaraLength.Tex

Tr
Conn.Open(
da.Fill(ds, "EditCara"
'display the records in the various control
ds.Tables("dtCaravan_Details").Select(
Me.txtTotCaraOnSite.DataBindings.Add(New Binding("Text", ds.Table
("EditCara"), "Caravan_Model")
Me.txtBeds.DataBindings.Add(New Binding("Text", ds.Tables("EditCara"), "Beds")
Me.txtPrice.DataBindings.Add(New Binding("Text", ds.Tables("EditCara"), "Cost")

Catch ex As Exceptio
MsgBox(ex.Message
Finall
Conn.Close(
End Tr

The problem im having is that i keep getting a message that states
Object reference not set to an instance of an objec

None of the controls display any results
Does anyone know what i am doin wrong
This is the first time im using the databind method so i need as much advise as poss :
Also once the controls are bound to the results of the query can i update the values in them
for example i want the user to be able to update the values in the textbox and then push the results back to the database using and u[pdate command. Can this be done through the use of the databinding method
 
G

Guest

Hi
I got the binding part of my code working. I made a typo in my coding. :blush:
Can anyone still answer my second question however....
Can i update the values that are in the bound textboxes and then have a save button to update the changes to the database?? what is the easiest way to do this? Is there another method to make this easier

Also another problem i have encountered is when i change the combobox again and this binding code is run a second time i get the following error message and the values in the bound textboxes do not change...
error message
This would cause two bindings in the collection to bind to the same property.
Parameter name: bindin
does anyone know what the problem could be

this is my code

Private Sub CBoxEditCaraLength_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxEditCaraLength.SelectedIndexChange

'Display the caravan details based on the length and the caravan model
Dim cmdEditCara As New SqlCommand("SPSelCaraDetailsOnModelLength", Conn
Dim da As New SqlDataAdapter(cmdEditCara
Dim ds As New DataSet(

'run a command to fill the textboxes et
cmdEditCara.CommandType = CommandType.StoredProcedur

'declare the parameter
cmdEditCara.Parameters.Add("@CaraModel", SqlDbType.Char, 50
cmdEditCara.Parameters.Add("@Length", SqlDbType.Char, 10
'now set the value
cmdEditCara.Parameters("@CaraModel").Value = CBoxEditCaraName.Tex
cmdEditCara.Parameters("@Length").Value = CBoxEditCaraLength.Tex

Tr
Conn.Open(

da.Fill(ds, "EditCara"
'display the records in the control
ds.Tables("EditCara").Select(
Me.txtTotCaraOnSite.DataBindings.Add(New Binding("Text", ds.Table
("EditCara"), "No_Of_Cara_Onsite")
Me.txtCaraName.DataBindings.Add(New Binding("Text", ds.Table
("EditCara"), "Caravan_Model")
Me.txtLength.DataBindings.Add(New Binding("Text", ds.Table
("EditCara"), "Length")
Me.txtBeds.DataBindings.Add(New Binding("Text", ds.Tables("EditCara"), "Beds")
Me.txtPrice.DataBindings.Add(New Binding("Text", ds.Tables("EditCara"), "Cost")
Catch ex As Exceptio
MsgBox(ex.Message
Finall
Conn.Close(
End Tr

End Su

thx
 
G

Guest

I have figured the problem out. :blush:
I found the answer in a book. I forgot to clear any existing bindings before recreating them

thx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top