Data Bind for textbox

G

Guest

I have bind a dataset, DentistDataSet1, to TextBox, txtLastName, by set the
textbox property (databindings--> simple binding--> ... -->LastName) and also
bind it to a dropdown list, DropdownList1 with datasource (DentistDataSet1),
DataMember (Dentists), and DataTextField (DentistID), and AutoPostBack(true).
The settings are done by Visual .NET studio 2003.

Also I have the following event code and expect the FirstName and LastName
would be changed with selecting another DentistID in the dropdown list.
-------
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DropDownList1.SelectedIndexChanged

txtLastName.Text =
DentistDataSet1.Tables(0).Rows(DropDownList1.SelectedIndex)("LastName")
txtFirstName.Text =
DentistDataSet1.Tables(0).Rows(DropDownList1.SelectedIndex)("FirstName")

End Sub
------

When I select a new DentistID, I got the following error:
------
Exception Details: System.IndexOutOfRangeException: There is no row at
position 1.

Source Error:


Line 182: 'txtLastName.DataBind()
Line 183: 'txtFirstName.DataBind()
Line 184: txtLastName.Text =
DentistDataSet1.Tables(0).Rows(DropDownList1.SelectedIndex)("LastName")
Line 185: txtFirstName.Text =
DentistDataSet1.Tables(0).Rows(DropDownList1.SelectedIndex)("FirstName")
Line 186:
------
Any one could help me to figure out the problem? Or what is the correct
steps to set up the properties and coding to get my expected result?

Thank you very much

David
 
K

Karl Seguin

You have to refill your dataset on postback...it doesn't magically preserve
its values...

Karl
 
G

Guest

Thank you, Karl:
I have added code to Page_load as follows, but I still got error.
-----------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
SqlDataAdapter1.Fill(DentistDataSet1)
DropDownList1.DataBind()
txtLastName.DataBind()
txtFirstName.DataBind()
txtLastName.Text =
DentistDataSet1.Tables(0).Rows(DropDownList1.SelectedIndex)("LastName")
txtFirstName.Text =
DentistDataSet1.Tables(0).Rows(DropDownList1.SelectedIndex)("FirstName")



End If

SqlDataAdapter1.Fill(DentistDataSet1)
'DropDownList1.DataBind()
txtLastName.Text =
CStr(DentistDataSet1.Tables(0).Rows(CInt(lblCurrentIndex.Text))("LastName"))
txtFirstName.Text =
CStr(DentistDataSet1.Tables(0).Rows(CInt(lblCurrentIndex.Text))("FirstName"))

End Sub
----------------------
Exception Details: System.FormatException: Input string was not in a correct
format.

Source Error:


Line 178: SqlDataAdapter1.Fill(DentistDataSet1)
Line 179: 'DropDownList1.DataBind()
Line 180: txtLastName.Text =
CStr(DentistDataSet1.Tables(0).Rows(CInt(lblCurrentIndex.Text))("LastName"))
Line 181: txtFirstName.Text =
CStr(DentistDataSet1.Tables(0).Rows(CInt(lblCurrentIndex.Text))("FirstName"))
Line 182:

-------------------------
 
K

Karl Seguin

My guess is that lblCurrentIndex.Text isn't an int like you think it is...

Karl
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top