PDA

Click to See Complete Forum and Search --> : Page_load


hpl
Jun 24th, 2003, 08:25 AM
I have this code, which works fine in my Page_load. But if I try to run the sub from SelectedIndexChanged then nothing happens. Do I have to specify something for my label like 'autopostback=true' for drpLists?

Protected Sub bindKappeDimension()
If Not Page.IsPostBack Then
Dim strCn As String = "Provider=Microsoft.jet.oledb.4.0;"
strCn += "Data Source=" & Server.MapPath("priser04.MDB") & ";"
Dim strSql As String = "Select Dim,Serie1 FROM SerieMat WHERE matId=" & intMatNr
Dim strResultsHolder As Integer

Dim objConnection As New OleDbConnection(strCn)
Dim objCommand As New OleDbCommand(strSql, objConnection)
Dim objDataReader As OleDbDataReader

objConnection.Open()
objDataReader = objCommand.ExecuteReader()

Dim medieDim As Integer
medieDim = CInt(Dim1.SelectedItem.Text)

Do While objDataReader.Read() = True
strResultsHolder = objDataReader("Dim")
kappe1.Text = strResultsHolder
Loop


objDataReader.Close()
objConnection.Close()
End If
End Sub

Cander
Jun 24th, 2003, 09:05 AM
Because it causes a post back when SelectIndexChanged fires.

fungi
Jun 24th, 2003, 09:08 AM
I usually only use IsPostback within Page_Load so that certain code is only run the 1st time the form is loaded.

In your case, since the page has already been loaded at the time SelectedIndexChanged fires, the code inside the If Not IsPostback block will be bypassed.

Does this make sense?

hpl
Jun 24th, 2003, 02:03 PM
Hi, yes it does. Sorry, I just added the If Not IS.... when I tried to troubleshoot the sub. If I delete this nothing still happens. It has something to do with the Do While I think.

fungi
Jun 24th, 2003, 02:28 PM
Well do you know for sure that the sub is called by SelectedIndexChanged? If you're not using Vis Studio and cannot debug try using response.write().

You only need the AutoPostBack="True" if you want to post as soon as a new list item is selected.

Also as you suggest it could well be that no rows are returned for the new query. It all depends on what intMatNr is...

hpl
Jun 25th, 2003, 03:40 AM
It worked when I use a Do While Loop and used Exit Do at the right place. But you were right, I've forgot to set the intMatNr. Thanks :D