Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
AccessDataSource1.Update()
FormView1.ChangeMode(FormViewMode.ReadOnly)
Your code looks good, but there's only one thing wrong. The AccessDatasource control uses the Jet Oledb 4.0 driver, which means that it won't work with Access 2007 and higher (*.accdb) files. In order to make it work, convert your database to an Access 2003
file (*.mdb), or use a SqlDataSource instead
Since you've defined the Update statement as well as UpdateParameters. And you've put "Update" button inside the EditTemplate, You don't need to manually call AccessDataSource1.Update().
Of course……Well, if you want to manually do update, plz have a try like this:
AccessDataSource2.UpdateParameters["TrainCode"].DefaultValue = (FormView2.FindControl("TrainCodeTextBox") as TextBox).Text; AccessDataSource2.UpdateParameters["ColorValue"].DefaultValue = (FormView2.FindControl("ColorValueTextBox") as TextBox).Text; AccessDataSource2.UpdateParameters["TrainID"].DefaultValue = (FormView2.FindControl("TrainID") as Label).Text; AccessDataSource2.Update(); FormView2.ChangeMode(FormViewMode.ReadOnly);
Since you've defined the Update statement as well as UpdateParameters. And you've put "Update" button inside the EditTemplate, You don't need to manually call AccessDataSource1.Update().
No he doesn't have to (however, this update is coded inside antother button), but this is NOT the reason the update fails. It is because Access 2007 files (.accdb) do not work with an AccessDataSource!
I already gave you the 2 options in my first reply:
hans_v
In order to make it work, convert your database to an Access 2003 file (*.mdb), or use a SqlDataSource instead
But by the way, why can't you convert your Access 2007 (.accdb) file into a Access 2003 (.mdb) file? And are you sure the ACE 12.0 oledb driver is installed on you webserver as well?
In the onclik event of your cancel button you're calling the button_click event, in which you ccall the Update method of the datasource, so you're trying to update in the cancel button as well. I don't think this is what you want?
Protected Sub Button2_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
Dim connectionString As String = "provider=Microsoft.ACE.OLEDB.12.0;" + "data source=" + Page.Server.MapPath("App_Data\Form_ContinuousSimulated.mdb")
Dim conn As New System.Data.OleDb.OleDbConnection(connectionString)
Dim cmd As System.Data.OleDb.OleDbCommand = conn.CreateCommand()
conn.Open()
cmd = New System.Data.OleDb.OleDbCommand("UPDATE [T_Trains] SET [TrainCode]=TrainCodex,[ColorValue]=ColorValuex WHERE [TrainID]=1", conn)
ashrafedes
Member
10 Points
8 Posts
accessdatasouce1.update using VB code in Asp.net
Nov 04, 2011 01:24 AM|LINK
Dear Mr
I have problem , I add Button in Edit templete in form view1 and I put command accessdatasouce1.update . I find it is not working
I found No update happend and also No error I get it , I expect the command should be run from Accessdatasouce updatecommand specified in Aspx
please need your help , what is the missing ?
See the real code
ASPX code
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/DB.accdb"
DeleteCommand="DELETE FROM [cargo] WHERE [código] = ?"
InsertCommand="INSERT INTO [cargo] ([código], [cargo]) VALUES (?, ?)"
SelectCommand="SELECT * FROM [cargo]"
UpdateCommand="UPDATE [cargo] SET [cargo] = ? WHERE [código] = ?">
<DeleteParameters>
<asp:Parameter Name="código" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="código" Type="Int32" />
<asp:Parameter Name="cargo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="cargo" Type="String" />
<asp:Parameter Name="código" Type="Int32" />
</UpdateParameters>
</asp:AccessDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="código"
DataSourceID="AccessDataSource1">
<EditItemTemplate>
código:
<asp:Label ID="códigoLabel1" runat="server" Text='<%# Eval("código") %>' />
<br />
cargo:
<asp:TextBox ID="cargoTextBox" runat="server" Text='<%# Bind("cargo") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button" />
</EditItemTemplate>
<InsertItemTemplate>
cargo:
<asp:TextBox ID="cargoTextBox" runat="server" Text='<%# Bind("cargo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
código:
<asp:Label ID="códigoLabel" runat="server" Text='<%# Eval("código") %>' />
<br />
cargo:
<asp:Label ID="cargoLabel" runat="server" Text='<%# Bind("cargo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
VB :
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
AccessDataSource1.Update()
FormView1.ChangeMode(FormViewMode.ReadOnly)
End Sub
ashrafedes
Member
10 Points
8 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 04, 2011 09:50 AM|LINK
hans_v
All-Star
35986 Points
6550 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 04, 2011 11:05 AM|LINK
Your code looks good, but there's only one thing wrong. The AccessDatasource control uses the Jet Oledb 4.0 driver, which means that it won't work with Access 2007 and higher (*.accdb) files. In order to make it work, convert your database to an Access 2003 file (*.mdb), or use a SqlDataSource instead
ashrafedes
Member
10 Points
8 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 05, 2011 07:55 AM|LINK
I change from ACCDB to MDB and I get Exactlly the same result
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/Form_ContinuousSimulated.mdb"
DeleteCommand="DELETE FROM [T_Trains] WHERE [TrainID] = ?"
InsertCommand="INSERT INTO [T_Trains] ([TrainID], [TrainCode], [ColorValue]) VALUES (?, ?, ?)"
SelectCommand="SELECT * FROM [T_Trains]"
UpdateCommand="UPDATE [T_Trains] SET [TrainCode] = ?, [ColorValue] = ? WHERE [TrainID] = ?">
<DeleteParameters>
<asp:Parameter Name="TrainID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="TrainID" Type="Int32" />
<asp:Parameter Name="TrainCode" Type="String" />
<asp:Parameter Name="ColorValue" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="TrainCode" Type="String" />
<asp:Parameter Name="ColorValue" Type="Int32" />
<asp:Parameter Name="TrainID" Type="Int32" />
</UpdateParameters>
</asp:AccessDataSource>
</p>
<asp:FormView ID="FormView2" runat="server" DataKeyNames="TrainID"
DataSourceID="AccessDataSource2">
<EditItemTemplate>
TrainID:
<asp:Label ID="TrainIDLabel1" runat="server" Text='<%# Eval("TrainID") %>' />
<br />
TrainCode:
<asp:TextBox ID="TrainCodeTextBox" runat="server"
Text='<%# Bind("TrainCode") %>' />
<br />
ColorValue:
<asp:TextBox ID="ColorValueTextBox" runat="server"
Text='<%# Bind("ColorValue") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click1"
Text="Button" />
</EditItemTemplate>
<InsertItemTemplate>
TrainCode:
<asp:TextBox ID="TrainCodeTextBox" runat="server"
Text='<%# Bind("TrainCode") %>' />
<br />
ColorValue:
<asp:TextBox ID="ColorValueTextBox" runat="server"
Text='<%# Bind("ColorValue") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
TrainID:
<asp:Label ID="TrainIDLabel" runat="server" Text='<%# Eval("TrainID") %>' />
<br />
TrainCode:
<asp:Label ID="TrainCodeLabel" runat="server" Text='<%# Bind("TrainCode") %>' />
<br />
ColorValue:
<asp:Label ID="ColorValueLabel" runat="server"
Text='<%# Bind("ColorValue") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
VB
Protected Sub Button2_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
AccessDataSource2.Update()
FormView2.ChangeMode(FormViewMode.ReadOnly)
End Sub
How can I update using VB by using accessdatasource . updateparamenter.add ..
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 06, 2011 01:12 AM|LINK
Hello ashrafedes :)
Since you've defined the Update statement as well as UpdateParameters. And you've put "Update" button inside the EditTemplate, You don't need to manually call AccessDataSource1.Update().
Of course……Well, if you want to manually do update, plz have a try like this:
AccessDataSource2.UpdateParameters["TrainCode"].DefaultValue = (FormView2.FindControl("TrainCodeTextBox") as TextBox).Text;
AccessDataSource2.UpdateParameters["ColorValue"].DefaultValue = (FormView2.FindControl("ColorValueTextBox") as TextBox).Text;
AccessDataSource2.UpdateParameters["TrainID"].DefaultValue = (FormView2.FindControl("TrainID") as Label).Text;
AccessDataSource2.Update();
FormView2.ChangeMode(FormViewMode.ReadOnly);
hans_v
All-Star
35986 Points
6550 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 10, 2011 06:58 AM|LINK
No he doesn't have to (however, this update is coded inside antother button), but this is NOT the reason the update fails. It is because Access 2007 files (.accdb) do not work with an AccessDataSource!
ashrafedes
Member
10 Points
8 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 10, 2011 07:04 AM|LINK
I already have Access Database .accdb , for Website , I can not change this Database , so what is the solution ?
hans_v
All-Star
35986 Points
6550 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 10, 2011 07:06 AM|LINK
I already gave you the 2 options in my first reply:
But by the way, why can't you convert your Access 2007 (.accdb) file into a Access 2003 (.mdb) file? And are you sure the ACE 12.0 oledb driver is installed on you webserver as well?
hans_v
All-Star
35986 Points
6550 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 10, 2011 07:17 AM|LINK
By the way, I noticed something:
In the onclik event of your cancel button you're calling the button_click event, in which you ccall the Update method of the datasource, so you're trying to update in the cancel button as well. I don't think this is what you want?
ashrafedes
Member
10 Points
8 Posts
Re: accessdatasouce1.update using VB code in Asp.net
Nov 10, 2011 08:00 AM|LINK
Look About This code
Protected Sub Button2_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
Dim connectionString As String = "provider=Microsoft.ACE.OLEDB.12.0;" + "data source=" + Page.Server.MapPath("App_Data\Form_ContinuousSimulated.mdb")
Dim conn As New System.Data.OleDb.OleDbConnection(connectionString)
Dim cmd As System.Data.OleDb.OleDbCommand = conn.CreateCommand()
conn.Open()
cmd = New System.Data.OleDb.OleDbCommand("UPDATE [T_Trains] SET [TrainCode]=TrainCodex,[ColorValue]=ColorValuex WHERE [TrainID]=1", conn)
cmd.Parameters.AddWithValue("TrainCodex", DirectCast(FormView2.FindControl("TrainCodeTextBox"), TextBox).Text)
cmd.Parameters.AddWithValue("ColorValuex", CInt(DirectCast(FormView2.FindControl("ColorValueTextBox"), TextBox).Text))
cmd.Parameters.AddWithValue("TrainIDx", DirectCast(FormView2.FindControl("TrainIDLabel1"), Label).Text)
cmd.ExecuteNonQuery()
FormView2.ChangeMode(FormViewMode.[ReadOnly])
conn.Close()
End Sub
This make update manually by Connection string Defined right ?