Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I update a Dataset? 1

Status
Not open for further replies.

chrigil

Programmer
Sep 23, 2003
178
GB
I am populating a DropDownList via a Dataset. When the user selects an Item in the DDL he is given the option of deleting this record from the Database.
The problem I am experiencing is that when the user clicks the DELETE button it calls a Sub which deletes the record as the page reloads.
When the page reloads the deleted item is still in the list until the page is properly reloaded i.e. not a postback.

Is this a problem with the Dataset needing refreshing or is the page reloading somehow before the item is deleted i.e. the Sub called from the DELETE button is happening after the page loads the DDL's and if this is the case then why does the data keep reloading into the DDL on every PostBack? Although ASP.Nets ability to Keep State is really good for some htings it also brings with it a whole bunch of other problems. It's like having a cache from hell :)

You can probably guess by my questions that I'm newish to ASP.Net so any help would be appreciated.

Thanks in advance,

Chris
 
If you look at the order that a page is run in when a buton is clicked, you will notice that the page load runs first and then the button click event. So in your case what happens is:

1) User clicks the button
2) The Page loads
3) The item is removed from the database

As the list is bound in step 2, the item still exists then and is therefore displayed.

One way around it would be to Response.Redirect back to the page at the end of the button click event.

Oh, and you can't be that new to ASP.NET as your first post on this forum was over a year ago! [smile]

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Hi, thanks for the reply and yes your right, I did post here over a year ago. I guess I'll just have to admit to a lack of knowledge ;-)

With regards to the Redirect to make the DDL update I have tried this and although it does redirect it doesn't reflect the changes.

Correct me if I have interpretted it incorrectly but I thought that a DataSet was a disconnected container on the server that serves as a kind of efficient Database. Therefore could this be the problem? When I delete the item I am deleting from the Database but upon postback I am using the dataset?
Is this a possible cause?

Like I said before the only way I can get the changes to be reflected in the DDL is by either reloading the page via a link or by clicking in the address bar abd pressing return; a response.redirect() doesn't refresh it.



Thanks in advance,

Chris
 
With regards to the Redirect to make the DDL update I have tried this and although it does redirect it doesn't reflect the changes.
It should if the databind and rthe redirect are in the right places. Can you post your code where you attempt this and we can see if there are any errors?



----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Thanks again, here is the code. It has been shortened for obvious reasons but the important bits are there and there is the total page load --> redirect course of actions listed in order. Like I said before, the redirect works ok but the DDL doesn't update.



Sub PopulateDDL(calledFrom)
if Not isPostBack then

if calledfrom = "editEyewearMenu" then
fillsmDDL()
TitleLbl.text = "Please choose an Eyewear Menu Item to EDIT:"
end if
end if
End Sub

Sub fillsmDDL()

Dim DBConn as OleDbConnection
Dim smListRS As New OleDbDataAdapter
Dim DSsmList as New DataSet
Dim NumRecords as Integer

DBConn = New OleDbConnection(ConfigurationSettings.AppSettings("DBConnString"))
smListRS = New OleDbDataAdapter("SELECT smTitle, smID FROM smTable where mmID = 2 AND smID <> 1", DBConn)
smListRS.Fill(DSsmList,"smData")

smDDL.DataSource = DSsmList.Tables("smData").defaultView
smDDL.datatextfield="smTitle"
smDDL.datavaluefield="smID"
smDDL.DataBind()

smDDL.Items.insert(0,"Select Menu")
SubMenuPnl.visible = true
End Sub

Sub smChosen(sender As Object, e As System.EventArgs)

if HiddenCalledFrom.value = "editEyewearMenu" then
editEyewearMenuPnl.visible = true
editEyewearMenuTitleTBX.text = smDDL.SelectedItem.text
HiddenContentID.value = smDDL.SelectedItem.value
errorLBL.visible = false
end if
End Sub

Sub submitContent(sender As Object, e As System.EventArgs)

if HiddenCalledFrom.value = "editEyewearMenu" then

contentTitle = editEyewearMenuTitleTBX.text
contentTitle = Replace(contentTitle, "'", "&#39;")

if contentTitle = "" then
errorLBL.text = "Please Add a Menu Name"
return
elseif HiddenContentID.value = "Select Menu" then
errorLBL.text = "Please Select a Menu Name"
return
else
Dim UpdateContentConn As OleDBConnection
Dim UpdateCommand As New OleDBCommand
Dim sqlQuery as string
sqlQuery = "UPDATE smTable SET smTitle = '"& contentTitle &"' WHERE smID = "& HiddenContentID.value &""

updateContentConn = New OleDbConnection(ConfigurationSettings.AppSettings("DBConnString_Cav"))
UpdateCommand.CommandText = sqlQuery
UpdateCommand.CommandTimeout = 30
UpdateCommand.Connection = updateContentConn
UpdateCommand.Connection.Open
UpdateCommand.ExecuteNonQuery()
errorLBL.visible = false
end if

response.redirect(" & request.servervariables("REMOTE_ADDR") & request.servervariables("SCRIPT_NAME"))

end if
End Sub


Where am I going wrong?



Thanks in advance,

Chris
 
The problem I am experiencing is that when the user clicks the DELETE button it calls a Sub which deletes the record as the page reloads.
And where does this delete happen? I can't see any delete in the code above...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Sorry there is an add, edit and delete. As i mentioned in the last post i cut some code out for brevity. The edit is exactly the same though, when I edit a menu item the change isn't reflected in the DDL.

Sorry for the confusion.



Thanks in advance,

Chris
 
In that case it's simply because you only bind the drop down list if it's not a postback - when you click the edit/delete button it is a postback therefore the list does not get re-bound.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
The trouble with binding the list both initially and with PostBack is that if a user selects item 3 for instance, when the page reloads I want item 3 to be showing in the DDL but because it refreshes the DDL it shows the top item as selected.

Is the solution to repopulate on PostBack but set the Selected attribute to the DDL.SelectedItem.value so that it automatically loads with the correct item showing?

Would this work?



Thanks in advance,

Chris
 
The trouble with binding the list both initially and with PostBack is that if a user selects item 3 for instance, when the page reloads I want item 3 to be showing in the DDL but because it refreshes the DDL it shows the top item as selected.
This is getting confusing. I thought the idea was that the user would be deleting/editing a record? If they are deleting an item in the drop down list then it won't be there so you can't have it as the selected item in the DDL.

You know where the problem lies now (in the binding of the DDL) so how you actually want your application to be graphically shown (which item is selected etc) is entirely down to you.



----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Sorry for the confusion and thanks for the help ca8msm. I will add to your reputation/thank you for your help because you have been very helpful.

Basically I am able Add, Edit and Delete records. If I add a record there is no problem. The problem is when I edit or delete a record hence why my questions are seeming to appear nonsensical. If I edit a record I want it to appear edited in the DDL immediately and if I delete a record I want it to disappear immediately.

I still haven`t found a solution but I will keep trying.

Once again thanks for the help

Thanks in advance,

Chris
 
If you want to bind the DDL on the Page Load but only in certain circumstances (e.g. Delete and Edit) why don't you pass a querystring value back to the page when you do your response.redirect. You could then check the value of this querystring (and if the page is a postback) and then bind accordingly. e.g
Code:
    Sub PopulateDDL(ByVal calledFrom)
        If Not isPostBack Then

            If calledFrom = "editEyewearMenu" Then
                Select Case Request.QueryString("MyQueryString")
                    Case "EDIT"
                        fillsmDDL()
                    Case "DELETE"
                        fillsmDDL()
                    Case Else
                        ' Dont re-bind
                End Select
                fillsmDDL()
                TitleLbl.text = "Please choose an Eyewear Menu Item to EDIT:"
            End If
        End If
    End Sub

This is not exact but hopefully gives you an idea of what I am trying to explain.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top