Detailsview updading issue

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

HI

I currently use an access db as my datasource and use the following to bind
my data into my DetailsView below

Page_Load()
...
myDetailsView.DataSource = myOleDbCommand.ExecuteReader();
myDetailsView.DataBind();

<Body>
...
<asp:DetailsView ID="myDetailsView" runat="server"
AutoGenerateRows="False">
<asp:BoundField DataField="FieldA" headertext="FieldA"
SortExpression="FieldA" />
<asp:BoundField DataField="FieldB" headertext="FieldB"
SortExpression="FieldB" />
...

How would I configure my event handler to have my dataview goes into Edit
mode and update my binded records into my access db ?

Thanks
Peter
 
HI

I currently use an access db as my datasource and use the following to
bind my data into my DetailsView below

Page_Load()
..
myDetailsView.DataSource = myOleDbCommand.ExecuteReader();
myDetailsView.DataBind();

<Body>
..
<asp:DetailsView ID="myDetailsView" runat="server"
AutoGenerateRows="False">
<asp:BoundField DataField="FieldA" headertext="FieldA"
SortExpression="FieldA" />
<asp:BoundField DataField="FieldB" headertext="FieldB"
SortExpression="FieldB" />
..

How would I configure my event handler to have my dataview goes into
Edit mode and update my binded records into my access db ?


First, have the Page_Load call a routine. Standard is this:

Proected Sub Page_Load(sender as Object, e as EventArgs)
_Handles Page.Load

If Not Page.IsPostBack() Then
BindPage()
End If

End Sub

Protected Sub BindPage()

...

myDetailsView.DataSource = myOleDbCommand.ExecuteReader();
myDetailsView.DataBind()
End Sub

This allows you to call the row on the events that will change content
of the data grid. You then call the same on the events that add, update
and delete rows.

When you use the automagic wiring bits, you may have to wire up some
events. I know RowsAdded and RowsRemoved can be used. I am not sure
about edit off hand, but I generally will edit using a DetailsView (user
clicks on row and it goes to Details View), as it focus the user on the
row rather than editing like a spreadsheet.It has the following events
to handle:

ItemDeleted
ItemInserted
ItemUpdated

You can also use ItemDeleting if you want to stop a person from deleting
certain information. Just set e.Cancel to true to stop the action during
this event.

Hope this helps!


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Thanks Gregory but I decided to use the DetailsView for editing my records and everything works perfectly with the following.

---
<asp:AccessDataSource ID="myProductTable" runat="Server" DataFile="/abc.mdb" SelectCommand="SELECT * FROM table1"
UpdateCommand="UPDATE table1 SET name = @name WHERE username = @username"
DeleteCommand="DELETE FROM table1 WHERE (username = @username)" InsertCommand="INSERT INTO table1(username, name)
VALUES (@username, @name)">
---

But when I want to leave out the "DataFile" property and dynamically assign it in my page_load module. It works good when in the
ReadOnly mode and also allows me to click Edit and New link button, but it seems cannot works for Update, Delete and Insert. The
following exception happen when I click the "Update", "Delete" and "Insert" button.

---
System.Data.OleDb.OleDbException: No error message available, result code: DB_SEC_E_AUTH_FAILED(0x80040E4D).
---

Can you give me a hand on how I can dynamically assign the DataFile property to work perfectly ?

Thanks
Peter
 
Also, can I do any validation on the field values before it's being updated to avoid data type mismatch on any fields ?
 
Thanks Gregory but I decided to use the DetailsView for editing my
records and everything works perfectly with the following.

---
<asp:AccessDataSource ID="myProductTable" runat="Server"
DataFile="/abc.mdb" SelectCommand="SELECT * FROM table1"
UpdateCommand="UPDATE table1 SET name = @name WHERE username =
@username"
DeleteCommand="DELETE FROM table1 WHERE (username =
@username)" InsertCommand="INSERT INTO
table1(username, name)
VALUES (@username, @name)">
---

But when I want to leave out the "DataFile" property and dynamically
assign it in my page_load module. It works good when in the ReadOnly
mode and also allows me to click Edit and New link button, but it
seems cannot works for Update, Delete and Insert. The following
exception happen when I click the "Update", "Delete" and "Insert"
button.

---
System.Data.OleDb.OleDbException: No error message available, result
code: DB_SEC_E_AUTH_FAILED(0x80040E4D). ---

Can you give me a hand on how I can dynamically assign the DataFile
property to work perfectly ?


Any time you step outside of the box, you can get burned by MS.

Most likely you are missing some quote mark or you are using an @ and the
string is not being properly translated.

One way around this is to always set the connection string explicitly. As I
do not work with the DataSource objects very often, I am not sure where to
look for an answer to the question. I will have to look that one up later.

As for validation, you are probably best to do validation on the server
side and attach the validation to the events ending in -ing. For example
ItemUpdating instead of ItemUpdated. This allows you to set e.Cancel to
true when it fails validation. The basic pattern is:

If Not (Validated()) Then
e.Cancel = True
End If

i will see if I can find anything for the DataSource objects on conneciton
string.


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Hi Gregory

Thanks for your help.. Referring to my error when updating the access
database file, it defintely something with the DataFile assignment and I am
not sure WHERE I should re-assign the .DataFile property. The first time
it was assigned it works perfectly for displaying the record for the first
time but not when performing any updating. If I include the
"DataFile="/abc.mdb" in the AccessDataSource line, everything works
perfectly. Any ideas ?

I will play around with the ItemUpdating event and see ....thanks :)

Peter
 
Hi Gregory

Thanks for your help.. Referring to my error when updating the
access database file, it defintely something with the DataFile
assignment and I am not sure WHERE I should re-assign the .DataFile
property. The first time it was assigned it works perfectly for
displaying the record for the first time but not when performing any
updating. If I include the "DataFile="/abc.mdb" in the
AccessDataSource line, everything works perfectly. Any ideas ?

I will play around with the ItemUpdating event and see ....thanks :)


Access is known to be problematic when deployed. If you are always explicit
on file location, it is less problematic.

If you have the option of moving to SQL Express, I would go that route over
Access, as Access has too many limitations for web use. That is, at least,
my opinion on the matter.


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top