473,769 Members | 6,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot Update Database through DataGrid & DataList Control

tjc0ol
26 New Member
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 1782
r035198x
13,262 MVP
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 New Member
A million thanks ;-)
Jul 18 '08 #3

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

Similar topics

0
305
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 when it gets to the section: strgame_score = game_score1.Text it says Object reference not set to an instance of an object. Here is my code, can anyone help?
1
5291
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 the user doesn't have to scroll horizontally. Essentially, I want to wrap a datagrid row (not text in individual columns) with as much control as possible. Make sense? While this seems simple enough on the surface, I can't figure out how to do...
0
1518
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 to in my Itemcommand event of the datalist. When I press a button on the datalist, that brings me into the Itemcommand event. I have access to the datagrid control, but not any column controls on the datagrid. For instance, my first column is...
25
4073
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
5
2437
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
4237
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
0
1703
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 OnEditCommand event is raised, I try to do the following: 1. Find the selected datalist item in which the nested datagrid raised the event. (by calling getDataGridReference and returning the DataList's selectedIndex)
0
1164
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 the EditItemTemplate part): <EditItemTemplate> <table cellpadding="10" style="font: 12PT Bookman Old Style"> <tr>
1
1559
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
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.