hello,
i am new to asp.net. i am trying to do a simple application of adding,insertin g and deleting rows in gridview. i am using C#.net and javascript.
My edit/update works but i have problems in deleting the rows in the datagrid. i have used command fields(properti es-columns) edit,update,can cel and delete commands. i changed it to template field. both are as buttons. ihave changed the id of Delete command to btDelete.
The following is the code for edit and update THIS CODE WORKS -
protected void GridView2_RowUpdating(object sender,GridViewUpdateEventArgs e)
-
{
-
-
String S1;SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
-
MyCon.Open();
-
-
Button b1 = new Button();
-
-
TextBox t1 = new TextBox();
-
TextBox t2 = new TextBox();
-
-
foreach (GridViewRow gv in GridView2.Rows)
-
{
-
-
b1 = (Button)gv.FindControl("button1");
-
t2 = (TextBox)gv.FindControl("TextBox2");
-
-
t1 = (TextBox)gv.FindControl("TextBox1");if (b1.CommandName == "Update")
-
{
-
-
-
-
S1 = "update t1 set name='" + t2.Text + "' where id=" + t1.Text + "";
-
SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
-
-
Response.Write("updated");
-
UpdateCmd.ExecuteNonQuery();
-
-
}
-
-
}
-
-
MyCon.Close();
-
-
-
-
}
=============== ====== -
protected void GridView2_RowEditing(Object sender, GridViewEditEventArgs e)
-
{
-
-
-
-
GridView2.EditIndex = e.NewEditIndex;
-
-
Display();
-
-
}
-
-
-
-
public void Display()
-
{
-
-
-
-
SqlConnection GridCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
-
GridCon.Open();
-
-
SqlDataAdapter MyDa = new SqlDataAdapter("select ID,name,date,qty,price,amount from t1", GridCon);
-
DataSet MyDs = new DataSet();
-
-
DataTable MyDt = new DataTable();
-
MyDa.Fill(MyDt);
-
-
GridView2.DataSource = MyDt;
-
-
GridView2.DataBind();
-
-
}
=============== ======
This is the code for delete WHICH DOES NOT WORK -
protected void GridView2_RowDeleting(object sender,GridViewDeleteEventArgs e)
-
-
-
{
-
-
-
-
String S1;SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
-
MyCon.Open();
-
-
Button b1 = new Button();
-
-
-
foreach (GridViewRow gd in GridView2.Rows)
-
{
-
-
b1 = (Button)gd.FindControl("btDelete");
-
-
if (b1.CommandName=="Delete")
-
{
-
-
S1 = "delete from demo where id=" + gd.Cells[0].Text;
-
-
SqlCommand DeleteCmd = new SqlCommand(S1, MyCon);
-
DeleteCmd.ExecuteNonQuery();
-
-
Response.Write("deleted");
-
}
-
-
}
-
-
}
-
-
This is the code on asp side -
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Style="z-index: 104;
-
left: 66px; position: absolute; top: 405px" Width="581px" OnRowEditing="GridView2_RowEditing"
-
-
OnRowDeleting="GridView2_RowDeleting" OnRowUpdating="GridView2_RowUpdating"
-
-
-
OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
ANY HELP PLEASE
2 2454
What error are you getting?
What error are you getting?
thanks for replying
i have sorted out the problem
it seems i missed out many points which i chalk it out.
1. In the asp side code i was supposed to add
<asp:GridView DataKeyNames="I d"
This i was not aware of.
2. this is the code i wrote in delete - protected void GridView2_RowDeleting(object sender,GridViewDeleteEventArgs e)
-
-
{
-
-
int ID = (int)GridView2.DataKeys[e.RowIndex].Value;
-
-
String S1;
-
SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
-
MyCon.Open();
-
S1 = "delete from t1 where id=" + ID;
-
SqlCommand Deletecmd = new SqlCommand(S1, MyCon);
-
Deletecmd.ExecuteNonQuery();
-
-
}
Now this works perfectly.
I must confess that i am very much embarassed to let you know the time taken for me to do this little piece of coding
Thanks
cmrhema
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: NateDawg |
last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if
anyone has some input it would sure help me out.
Ok, I’ve noticed a few gridview problems floating around the forum. Everyone
wants to do a java confirmation box when a user clicks the delete button.
Fair enough, basic user design rules state that you should always confirm a
delete action. There is also a consensus that the best way to do this is a
template...
|
by: Jürgen Bayer |
last post by:
Hi,
I just tried out the ObjectDataSource of ASP.NET 2.0. A simple application
works with a GridView bound to an ObjectDataSource. The ObjectDataSource is
set to a (factory) class (PersonFactory) with static methods (Select,
Update, Delete, see below). The factory methods work with instances of a
simple class (Person, see below). Select and Update works, but the Person
object passed to the Delete method is always empty (Id == 0;...
|
by: JasonK |
last post by:
I would like to move the Delete button such that it displays one time in the
footer row, rather than on every row. I've seen lots of questions asked on
the subject around the net, but no answer that seems to solve the problem.
I have tried a few things:
- adding a button, setting it's command name to Delete. Problem with this
is that the CommandArgument value does not get set to the SelectedIndex of
the GridView. Instead it has the...
|
by: tarscher |
last post by:
Hi all,
I have a grid that contains 7 columns from 3 tables (3 unique keys, 4
normal fields). I show this 7 columns on the gridview. I now want to
add edit and delete functionality. This should be fairly easy cos it's
a build in feature of the gridview but alas...
When the user clicks delete only one row on one table needs to be
deleted thus I made a new query in my Datatable (DeleteissueQuery) that
excpects an id
|
by: Steve |
last post by:
I have a gridview which uses an objectdatasource for its select and
delete.
The delete command uses the function below. The delete itself works but
the extra logic which requires parameters AccountDebitID and
AccountCreditID does not work.
'Delete a transaction
Sub deletetrans(ByVal transactionid As Integer, ByVal transactionamount
| |
by: Wannabe |
last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything
working except the delete command. The page reloads except the row I am
trying to delete is still there. I believe it is something really easy, but I
cannot see it. The stored procedue works when run in QA. Can someone tell me
what I am doing wrong/missing that is keeping the delete command from working
in the gridview? Thank you.
I am trying to delete a row out of...
|
by: jobs |
last post by:
I've got a gridview that does not have a datasourceid assigned in the
markup.
I'd like to switch between two datasources in the codebehind. when I do
switch, I first reset the the
gridviewx.datasource = nothing and gridviewx.datasourceid = nothing
before setting it to gridviewx.datasource = newdatasoruceid and then
rebinding.
All appears to work well until I try to excute a delete command that
|
by: Barry L. Camp |
last post by:
Hi all,
Wondering if someone can help with a nagging problem I am having using
a GridView and an ObjectDataSource. I have a simple situation where I
am trying to delete a row from a table, but it doesn't seem to work at
all. Below is my ASP.NET page, and further below, my VB.NET method that
I am trying to call:
<div id="admin-faq" class="page">
<h2>Site FAQs (Frequently Asked Questions)</h2>
|
by: William LaMartin |
last post by:
On webform, I am populating a GridView from a SQLDatasource based on a MySQL
table named PIB.
There is no vb code involved. Everything is done in the source for the aspx
page, provided below. The update works fine, but a delete of a row produces
the following error:
Exception Details: System.Data.Odbc.OdbcException: ERROR
SQLBindParameter not used for all
parameters
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |