gleznov
asked on
Web App - Can't retrieve cached dataset
My problem is that I can't seem to get at a cached dataset. DS_Login1 is the dataset, and at page_load here's my code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not (IsPostBack) Or IsNothing(Cache.Get("DS_Lo gin")) Then
Dim connectionString As String
connectionString = "data source=ga1cdc02;initial catalog=crmdb"
connectionString &= ";Trusted_Connection=true"
SqlConnection1.ConnectionS tring = connectionString
SqlConnection1.Open() '?
SqlDataAdapter1.Fill(DS_Lo gin1)
For x As Integer = 0 To DS_Login1.Tables("users"). Rows.Count - 1
cmbUsers.Items.Add(DS_Logi n1.Tables( "users").R ows(x).Ite m("User"))
Next
Cache.Add("DS_Login", DS_Login1, Nothing, DateTime.MaxValue, System.TimeSpan.FromMinute s(20), Caching.CacheItemPriority. Default, Nothing)
Else
DS_Login1 = Cache("DS_Login")
lblPassWrong.Text = DS_Login1.Tables("users"). Rows(0).It em("user")
lblPassWrong.Visible = True
End If
End Sub
Under the else, I get the error of "There is no row at position -1" - so I know it's just not loading the cached dataset back into DS_Login1. I don't know why though - the code with the caching and the whole if/else/endif to do with (ispostback) is straight out of a Microsoft book.
Thanks!
JP
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not (IsPostBack) Or IsNothing(Cache.Get("DS_Lo
Dim connectionString As String
connectionString = "data source=ga1cdc02;initial catalog=crmdb"
connectionString &= ";Trusted_Connection=true"
SqlConnection1.ConnectionS
SqlConnection1.Open() '?
SqlDataAdapter1.Fill(DS_Lo
For x As Integer = 0 To DS_Login1.Tables("users").
cmbUsers.Items.Add(DS_Logi
Next
Cache.Add("DS_Login", DS_Login1, Nothing, DateTime.MaxValue, System.TimeSpan.FromMinute
Else
DS_Login1 = Cache("DS_Login")
lblPassWrong.Text = DS_Login1.Tables("users").
lblPassWrong.Visible = True
End If
End Sub
Under the else, I get the error of "There is no row at position -1" - so I know it's just not loading the cached dataset back into DS_Login1. I don't know why though - the code with the caching and the whole if/else/endif to do with (ispostback) is straight out of a Microsoft book.
Thanks!
JP
ASKER
OK, upping points to maximum - I can't continue my project until I get some assistance. I'm open to ANYTHING that will allow me to access a dataset after postback.
JP
JP
What's the row count value here when you run it : For x As Integer = 0 To DS_Login1.Tables("users"). Rows.Count - 1
racin
racin
ASKER
I'm starting to think it's not a dataset problem also - this may be really easy. I'm now filling the dataset from the button press code. Still same error. I think it must have something to do with the combo box?
JP
JP
ASKER
Racin: There are 8 rows, and I'm sure they're being accessed fine the first time because the combo box is filled with every user listed in the user column.
JP
JP
ASKER
OK, I just injected a label.text to be equal to the row.count for my table inside the button press call. And it displays 8, which is the correct number. But if I allow the code to continue
For x As Integer = 0 To DS_Login1.Tables("users"). Rows.Count - 1
If cmbUsers.SelectedValue Is DS_Login1.Tables("users") Then
selIndex = x
Exit For
End If
Next
If TxtPass.Text <> DS_Login1.Tables("users"). Rows(selIn dex).Item( "password" ) Then
lblPassWrong.Visible = True
Exit Sub
End If
Response.Redirect("MainMen u.aspx")
End Sub
then I get the -1 error.
JP
For x As Integer = 0 To DS_Login1.Tables("users").
If cmbUsers.SelectedValue Is DS_Login1.Tables("users") Then
selIndex = x
Exit For
End If
Next
If TxtPass.Text <> DS_Login1.Tables("users").
lblPassWrong.Visible = True
Exit Sub
End If
Response.Redirect("MainMen
End Sub
then I get the -1 error.
JP
ASKER
And that holds true without refilling the dataset in the button code.
JP
JP
does your combo box do a postback when you select something?
racin
racin
ASKER
This must be what I'm missing (I'm new to designing web apps) - what code do I need in the combo box to postback? What does it need to do?
JP
JP
What's selIndex when you pass it in to the dataset here:
If TxtPass.Text <> DS_Login1.Tables("users"). Rows(selIn dex).Item( "password" ) Then
If TxtPass.Text <> DS_Login1.Tables("users").
ASKER
selIndex (selected index) is fingered by which user record is pointed at, in:
Private Sub CmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLogin.Click
>>> Dim selIndex As Integer = -1
>>> For x As Integer = 0 To DS_Login1.Tables("users"). Rows.Count - 1
>>> If cmbUsers.SelectedValue Is DS_Login1.Tables("users") Then
>>> selIndex = x
>>> Exit For
>>> End If
>>> Next
If TxtPass.Text <> DS_Login1.Tables("users"). Rows(selIn dex).Item( "password" ) Then
lblPassWrong.Visible = True
Exit Sub
End If
Response.Redirect("MainMen u.aspx")
End Sub
So it looks through the dataset for the user that's the same as the one chosen in the combo box (or listbox or whatever it's called in web apps) and then sets the position in the dataset as the selected index selIndex, so that I can reference the password for that user using tables("users").rows(selIn dex).item( "password" )
JP
Private Sub CmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLogin.Click
>>> Dim selIndex As Integer = -1
>>> For x As Integer = 0 To DS_Login1.Tables("users").
>>> If cmbUsers.SelectedValue Is DS_Login1.Tables("users") Then
>>> selIndex = x
>>> Exit For
>>> End If
>>> Next
If TxtPass.Text <> DS_Login1.Tables("users").
lblPassWrong.Visible = True
Exit Sub
End If
Response.Redirect("MainMen
End Sub
So it looks through the dataset for the user that's the same as the one chosen in the combo box (or listbox or whatever it's called in web apps) and then sets the position in the dataset as the selected index selIndex, so that I can reference the password for that user using tables("users").rows(selIn
JP
I know ... but I'm wondering if it doesn't correctly match the cmbUsers.SelectedValue to DS_Login1.Tables("users") then selIndex = -1 when you get to here:
If TxtPass.Text <> DS_Login1.Tables("users"). Rows(selIn dex).Item( "password" ) Then
lblPassWrong.Visible = True
Exit Sub
End If
which is where I think you get the error.
To test this I would see what the count is at For x as integer = 0 to .count -1 and make sure that I have a count 8 still. Then I would break cmdUsers.SelectedValue into it's own variable and DS_Login1.Tables ... I think i found it...
DS_Login1.Tables("users") is not looking at the actual user's value but rather the whole table.
Try this:
DS_Login1.Tables("users"). Rows.(x).I tem("User" )
Racin
If TxtPass.Text <> DS_Login1.Tables("users").
lblPassWrong.Visible = True
Exit Sub
End If
which is where I think you get the error.
To test this I would see what the count is at For x as integer = 0 to .count -1 and make sure that I have a count 8 still. Then I would break cmdUsers.SelectedValue into it's own variable and DS_Login1.Tables ... I think i found it...
DS_Login1.Tables("users") is not looking at the actual user's value but rather the whole table.
Try this:
DS_Login1.Tables("users").
Racin
try it here :
>>> If cmbUsers.SelectedValue Is DS_Login1.Tables("users") Then
>>> If cmbUsers.SelectedValue Is DS_Login1.Tables("users") Then
ASKER
Oh good God.
That was a stupid error on my part - sorry. But the problem's still the same even after fixing it :( Here's the new sub:
Private Sub CmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLogin.Click
Dim selIndex As Integer = -1
lblPassWrong.Text = (DS_Login1.Tables("users") .Rows.Coun t)
lblPassWrong.Visible = True
'Exit Sub
For x As Integer = 0 To DS_Login1.Tables("users"). Rows.Count - 1
If cmbUsers.SelectedValue Is DS_Login1.Tables("users"). Rows(x).It em("user") Then
selIndex = x
Exit For
End If
Next
If TxtPass.Text <> DS_Login1.Tables("users"). Rows(selIn dex).Item( "password" ) Then
lblPassWrong.Visible = True
Exit Sub
End If
Response.Redirect("MainMen u.aspx")
End Sub
I have realized one other thing - the -1 is from selIndex, if I start it at -5, that's the error I get. I've seen errors that were just the same as -1 so I assumed it wasn't the selIndex, which was also stupid. stupid stupid stupid. So basically it's not finding the value in the dataset, which means I must have something wrong with my context. Is:
If cmbUsers.SelectedValue Is DS_Login1.Tables("users"). Rows(x).It em("user") Then
correct? I'm used to useing = instead of "is", but it told me it didn't use = for string. Maybe I did this line wrong?
JP
That was a stupid error on my part - sorry. But the problem's still the same even after fixing it :( Here's the new sub:
Private Sub CmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLogin.Click
Dim selIndex As Integer = -1
lblPassWrong.Text = (DS_Login1.Tables("users")
lblPassWrong.Visible = True
'Exit Sub
For x As Integer = 0 To DS_Login1.Tables("users").
If cmbUsers.SelectedValue Is DS_Login1.Tables("users").
selIndex = x
Exit For
End If
Next
If TxtPass.Text <> DS_Login1.Tables("users").
lblPassWrong.Visible = True
Exit Sub
End If
Response.Redirect("MainMen
End Sub
I have realized one other thing - the -1 is from selIndex, if I start it at -5, that's the error I get. I've seen errors that were just the same as -1 so I assumed it wasn't the selIndex, which was also stupid. stupid stupid stupid. So basically it's not finding the value in the dataset, which means I must have something wrong with my context. Is:
If cmbUsers.SelectedValue Is DS_Login1.Tables("users").
correct? I'm used to useing = instead of "is", but it told me it didn't use = for string. Maybe I did this line wrong?
JP
ASKER
Or rather I have something wrong with my compare (lol), not context.
JP
JP
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cmbUsers.SelectedValue.tos tring was all that was needed. Thank you so much!
JP
JP
Excellent. You're welcome.
Racin
ASKER
Private Sub CmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLogin.Click
Dim selIndex As Integer = -1
For x As Integer = 0 To DS_Login1.Tables("users").
If cmbUsers.SelectedValue Is DS_Login1.Tables("users") Then
selIndex = x
Exit For
End If
Next
If TxtPass.Text <> DS_Login1.Tables("users").
lblPassWrong.Visible = True
Exit Sub
End If
Response.Redirect("MainMen
End Sub
I read one article that says you have to programatically select an index in some controls or it defaults to -1 - but I don't think this is the case and if so, don't know how to fix it. I tried putting cmbUsers.selectedindex = 0 after the endif in the page_load sub.
JP