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: - OleDbConnection objConn = new OleDbConnection(
-
"Provider=Microsoft.Jet.OleDb.4.0;" +
-
"Data Source=D:\\ASPX\\" +
-
"data-x.mdb");
-
OleDbCommand objCmd;
-
OleDbDataReader objRdr;
-
String strCmd;
-
-
void Page_Load() {
-
if (!IsPostBack) {
-
BindData();
-
}
-
}
-
void BindData() {
-
objConn.Open();
-
if (Request.QueryString["id"] != null) {
-
objCmd = new OleDbCommand(
-
"SELECT * FROM Employees WHERE EmployeeID=" +
-
Request.QueryString["id"], objConn);
-
objRdr = objCmd.ExecuteReader();
-
dgAddressBookDetails.DataSource = objRdr;
-
dgAddressBookDetails.DataBind();
-
} else {
-
objCmd = new OleDbCommand("SELECT * FROM Employees", objConn);
-
objRdr = objCmd.ExecuteReader();
-
dgAddressBook.DataSource = objRdr;
-
dgAddressBook.DataBind();
-
}
-
objRdr.Close();
-
objConn.Close();
-
}
-
void dg_Edit(Object s, DataGridCommandEventArgs e) {
-
dgAddressBook.EditItemIndex = e.Item.ItemIndex;
-
BindData();
-
}
-
void dg_Cancel(Object s, DataGridCommandEventArgs e) {
-
dgAddressBook.EditItemIndex = -1;
-
BindData();
-
}
-
void dg_Update(Object s, DataGridCommandEventArgs e) {
-
int intEmployeeID;
-
String strName, strExtension;
-
-
intEmployeeID = (int)dgAddressBook.DataKeys[e.Item.ItemIndex];
-
strName = ((TextBox)e.Item.FindControl("txtName")).Text;
-
-
strCmd = "UPDATE Employees SET Name=@Name, " +
-
"WHERE EmployeeID=@EmployeeID";
-
objCmd = new OleDbCommand(strCmd, objConn);
-
objCmd.Parameters.Add("@Name", strName);
-
objCmd.Parameters.Add("@EmployeeID", intEmployeeID);
-
-
objConn.Open();
-
objCmd.ExecuteNonQuery();
-
objConn.Close();
-
-
dgAddressBook.EditItemIndex = -1;
-
BindData();
-
}
-
void dg_Delete(Object s, DataGridCommandEventArgs e) {
-
int intEmployeeID;
-
-
intEmployeeID = (int)dgAddressBook.DataKeys[e.Item.ItemIndex];
-
-
strCmd = "DELETE FROM Employees WHERE EmployeeID=@EmployeeID";
-
objCmd = new OleDbCommand(strCmd, objConn);
-
objCmd.Parameters.Add("@EmployeeID", intEmployeeID);
-
-
objConn.Open();
-
objCmd.ExecuteNonQuery();
-
objConn.Close();
-
-
dgAddressBook.EditItemIndex = -1;
-
BindData();
-
}
And my DataGrid settings below: - <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">
-
<ItemStyle Font-Names="Arial" Font-Size="Smaller" ForeColor="#000000" />
-
<HeaderStyle Font-Names="Arial" Font-Size="Smaller" Font-Bold="true" ForeColor="#FFFFFF" BackColor="#003366" />
-
<AlternatingItemStyle Font-Names="Arial" Font-Size="Smaller" BackColor="#CCCCCC" />
-
<Columns>
-
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
-
<asp:TemplateColumn>
-
<HeaderTemplate>Name</HeaderTemplate>
-
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "Name") %></ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="txtName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
-
<asp:RequiredFieldValidator ID="rfvName" Display="Dynamic" ErrorMessage="Name is required!" ControlToValidate="txtName" runat="server" />
-
</EditItemTemplate>
-
</asp:TemplateColumn>
-
<asp:BoundColumn DataField="Extension" HeaderText="Extension" ReadOnly="true" />
-
<asp:ButtonColumn ButtonType="LinkButton" Text="Delete" CommandName="Delete" />
-
</Columns>
-
</asp:DataGrid>
Hope to hear from you guys, thanks in advance. -Tj
2 1782 -
-
strCmd = "UPDATE Employees SET Name=@Name, " +
-
"WHERE EmployeeID=@EmployeeID";
You do have an error in your update statement. That comma after @Name should not be there.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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...
|
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...
|
by: Neo Geshel |
last post by:
This works:
<form>
<asp:TextBox id="name" />
<%= name.ClientID %>
</form>
But this DOES NOT work:
<form>
|
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
| |
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:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
|
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)
|
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>
|
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.
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |