473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cannot Update Database through DataGrid & DataList Control

tjc0ol
26
Hi guys,
Im just wondering that I cannot update my database throught datagrid datalist control, and the only thing works is that I can delete, cancel, edit but when I clicked update link there's an error that says: "Syntax error in UPDATE statement."

I wonder what's wrong with my code and how to correct this one?

Below are my codes:

Expand|Select|Wrap|Line Numbers
  1. OleDbConnection objConn = new OleDbConnection(
  2.     "Provider=Microsoft.Jet.OleDb.4.0;" +
  3.     "Data Source=D:\\ASPX\\" + 
  4.     "data-x.mdb");
  5. OleDbCommand objCmd;
  6. OleDbDataReader objRdr;
  7. String strCmd;
  8.  
  9. void Page_Load() {
  10.     if (!IsPostBack) {
  11.       BindData();
  12.     }
  13. }
  14. void BindData() {
  15.     objConn.Open();
  16.     if (Request.QueryString["id"] != null) {
  17.         objCmd = new OleDbCommand(
  18.             "SELECT * FROM Employees WHERE EmployeeID=" + 
  19.             Request.QueryString["id"], objConn);
  20.         objRdr = objCmd.ExecuteReader();
  21.         dgAddressBookDetails.DataSource = objRdr;
  22.         dgAddressBookDetails.DataBind();
  23.     }    else {
  24.         objCmd = new OleDbCommand("SELECT * FROM Employees", objConn);
  25.         objRdr = objCmd.ExecuteReader();
  26.         dgAddressBook.DataSource = objRdr;
  27.         dgAddressBook.DataBind();
  28.     }
  29.         objRdr.Close();
  30.         objConn.Close();
  31. }
  32. void dg_Edit(Object s, DataGridCommandEventArgs e) {
  33.     dgAddressBook.EditItemIndex = e.Item.ItemIndex;
  34.     BindData();
  35. }
  36. void dg_Cancel(Object s, DataGridCommandEventArgs e) {
  37.     dgAddressBook.EditItemIndex = -1;
  38.     BindData();
  39. }
  40. void dg_Update(Object s, DataGridCommandEventArgs e) {
  41.     int intEmployeeID;
  42.     String strName, strExtension;
  43.  
  44.     intEmployeeID = (int)dgAddressBook.DataKeys[e.Item.ItemIndex];
  45.     strName = ((TextBox)e.Item.FindControl("txtName")).Text;
  46.  
  47.     strCmd = "UPDATE Employees SET Name=@Name, " +
  48.         "WHERE EmployeeID=@EmployeeID";
  49.     objCmd = new OleDbCommand(strCmd, objConn);
  50.     objCmd.Parameters.Add("@Name", strName);
  51.     objCmd.Parameters.Add("@EmployeeID", intEmployeeID);
  52.  
  53.     objConn.Open();
  54.     objCmd.ExecuteNonQuery();
  55.     objConn.Close();
  56.  
  57.     dgAddressBook.EditItemIndex = -1;
  58.     BindData();
  59. }
  60. void dg_Delete(Object s, DataGridCommandEventArgs e) {
  61.     int intEmployeeID;
  62.  
  63.     intEmployeeID = (int)dgAddressBook.DataKeys[e.Item.ItemIndex];
  64.  
  65.     strCmd = "DELETE FROM Employees WHERE EmployeeID=@EmployeeID";
  66.     objCmd = new OleDbCommand(strCmd, objConn);
  67.     objCmd.Parameters.Add("@EmployeeID", intEmployeeID);
  68.  
  69.     objConn.Open();
  70.     objCmd.ExecuteNonQuery();
  71.     objConn.Close();
  72.  
  73.     dgAddressBook.EditItemIndex = -1;
  74.     BindData();
  75. }
And my DataGrid settings below:

Expand|Select|Wrap|Line Numbers
  1. <asp:DataGrid id="dgAddressBook" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="None" OnEditCommand="dg_Edit" OnCancelCommand="dg_Cancel" OnUpdateCommand="dg_Update" OnDeleteCommand="dg_Delete" DataKeyField="EmployeeID">
  2.         <ItemStyle Font-Names="Arial" Font-Size="Smaller" ForeColor="#000000" />
  3.         <HeaderStyle Font-Names="Arial" Font-Size="Smaller" Font-Bold="true" ForeColor="#FFFFFF" BackColor="#003366" />
  4.          <AlternatingItemStyle Font-Names="Arial" Font-Size="Smaller" BackColor="#CCCCCC" />
  5.          <Columns>
  6.         <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
  7.         <asp:TemplateColumn>
  8.         <HeaderTemplate>Name</HeaderTemplate>
  9.         <ItemTemplate><%# DataBinder.Eval(Container.DataItem, "Name") %></ItemTemplate>
  10.         <EditItemTemplate>
  11.             <asp:TextBox ID="txtName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
  12.             <asp:RequiredFieldValidator ID="rfvName" Display="Dynamic" ErrorMessage="Name is required!" ControlToValidate="txtName" runat="server" />
  13.         </EditItemTemplate>
  14.         </asp:TemplateColumn>
  15.         <asp:BoundColumn DataField="Extension" HeaderText="Extension" ReadOnly="true" />
  16.         <asp:ButtonColumn ButtonType="LinkButton" Text="Delete" CommandName="Delete" />
  17.          </Columns>
  18.          </asp:DataGrid>
Hope to hear from you guys, thanks in advance. -Tj
Jul 18 '08 #1
2 1762
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1.  
  2.     strCmd = "UPDATE Employees SET Name=@Name, " +
  3.         "WHERE EmployeeID=@EmployeeID";
You do have an error in your update statement. That comma after @Name should not be there.
Jul 18 '08 #2
tjc0ol
26
A million thanks ;-)
Jul 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: vjmehra | last post by:
I have a datalist within a datagrid and I am having trouble trying to get the update command working. I am only using the one for the datagrid, but this should also update the datalist, however...
1
by: Craig Banks | last post by:
If a row of data in a dataset has a lot of columns the row displaying the data in a datagrid will run way off the screen. What I'd like to do is display a row of data over several datagrid rows so...
0
by: bill yeager | last post by:
I have a datalist displaying parent information and a datagrid presenting child information. The data is being rendered just fine. The datagrid has template columns on it that I cannot gain access...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
5
by: Jay Villa | last post by:
Is it possible to update only few columns in datagrid during OnUpdateCommand event ? If so, could you help me .... -thanks Jay
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
0
by: Chris | last post by:
I've been searching all over and think I am close, but keep getting the error "Index out of range" when trying to reference a nested datagrid when an OnEditCommand event is raised. When the...
0
by: Jim in Arizona | last post by:
I'm trying to do a simple update of data but I can't get the code right. It's a datalist control that has the update/cancel/delete subroutines. Here's the broken down datalist aspx code (just...
1
by: weird0 | last post by:
What is the difference between a DataList control and DataGrid control ? How and where are DataLists used? Can somebody place some good links for its use.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.