473,378 Members | 1,360 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,378 software developers and data experts.

Problem in loading gridview after deletion using asp.net(C#)

29
Hi,
can anybody clear my doubt in asp.net using c#.
i am deleting record in database using gridview control. the problem here is that record gets deleted in database but the changes is not reflected in the gridview control.i also used "gridview1.Databind()" after delete query. what should i do now?
regards,
a.prabhu
Jun 15 '07 #1
4 2151
dip_developer
648 Expert 512MB
Hi,
can anybody clear my doubt in asp.net using c#.
i am deleting record in database using gridview control. the problem here is that record gets deleted in database but the changes is not reflected in the gridview control.i also used "gridview1.Databind()" after delete query. what should i do now?
regards,
a.prabhu
Please post your code snippet.....
Jun 15 '07 #2
Prabhua
29
Here is my code--

<asp:AccessDataSource ID="AccessDataSource1" DataFile="Database/thecliniq.mdb" SelectCommand="Select * from tempr" runat="server"> </asp:AccessDataSource>

<asp:GridView ID="GridView1" DataSourceID="AccessDataSource1" AutoGenerateColumns="false" DataKeyNames ="id" runat="server" HorizontalAlign="Left" Height="1px" Width="407px" EnableViewState="False">

<Columns>

<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckAll" onclick="return check_uncheck (this );" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl7" Visible="false" Text='<%# DataBinder.Eval (Container.DataItem, "id") %>' runat="server" />
<asp:CheckBox ID="deleteRec" runat="server" />
<asp:Label ID="addsymptoms" Visible="false" Text='<%# DataBinder.Eval (Container.DataItem, "additionalsymptoms") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="additionalsymptoms" HeaderText="Additional Symtoms" />
<asp:TemplateField HeaderText="select">
<ItemTemplate>
<asp:DropDownList ID="DropDownList3" DataSource='<%# GetCategoryDescriptions() %>' DataTextField="nam" DataValueField="nam" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" Width="9555px" HorizontalAlign="Center" />
</asp:GridView>




this is a code inside click event of delete button--

protected void Delete_Click(object sender, EventArgs e)
{
string gvIDs = "";
bool chkBox = false;
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox deleteChkBxItem = (CheckBox)gv.FindControl("deleteRec");
if (deleteChkBxItem.Checked)
{
chkBox = true;
gvIDs = gvIDs + ((Label)gv.FindControl("lbl7")).Text.ToString() + ",";

}
}
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Database/thecliniq.mdb") + ";";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();


if (chkBox)
{
Response.Write(gvIDs.Substring(0, gvIDs.LastIndexOf(",")));
string deleteOleDb = "DELETE from tempr WHERE id IN (" + gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")";
OleDbCommand cmd = new OleDbCommand(deleteOleDb, conn);
cmd.ExecuteNonQuery();
}
GridView1.DataBind();
conn.Close();
}
Jun 15 '07 #3
dip_developer
648 Expert 512MB
Here is my code--

<asp:AccessDataSource ID="AccessDataSource1" DataFile="Database/thecliniq.mdb" SelectCommand="Select * from tempr" runat="server"> </asp:AccessDataSource>

<asp:GridView ID="GridView1" DataSourceID="AccessDataSource1" AutoGenerateColumns="false" DataKeyNames ="id" runat="server" HorizontalAlign="Left" Height="1px" Width="407px" EnableViewState="False">

<Columns>

<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckAll" onclick="return check_uncheck (this );" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl7" Visible="false" Text='<%# DataBinder.Eval (Container.DataItem, "id") %>' runat="server" />
<asp:CheckBox ID="deleteRec" runat="server" />
<asp:Label ID="addsymptoms" Visible="false" Text='<%# DataBinder.Eval (Container.DataItem, "additionalsymptoms") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="additionalsymptoms" HeaderText="Additional Symtoms" />
<asp:TemplateField HeaderText="select">
<ItemTemplate>
<asp:DropDownList ID="DropDownList3" DataSource='<%# GetCategoryDescriptions() %>' DataTextField="nam" DataValueField="nam" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" Width="9555px" HorizontalAlign="Center" />
</asp:GridView>




this is a code inside click event of delete button--

protected void Delete_Click(object sender, EventArgs e)
{
string gvIDs = "";
bool chkBox = false;
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox deleteChkBxItem = (CheckBox)gv.FindControl("deleteRec");
if (deleteChkBxItem.Checked)
{
chkBox = true;
gvIDs = gvIDs + ((Label)gv.FindControl("lbl7")).Text.ToString() + ",";

}
}
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Database/thecliniq.mdb") + ";";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();


if (chkBox)
{
Response.Write(gvIDs.Substring(0, gvIDs.LastIndexOf(",")));
string deleteOleDb = "DELETE from tempr WHERE id IN (" + gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")";
OleDbCommand cmd = new OleDbCommand(deleteOleDb, conn);
cmd.ExecuteNonQuery();
}
GridView1.DataBind();
conn.Close();
}
Databind () method binds gridview with the datasource you supply....
where is the updated datasource after deletion????? or am I overlooking something??
Jun 15 '07 #4
Prabhua
29
By default Gridview control will take data from Accessdatasource control,right? which is indicated in .aspx file.
Jun 15 '07 #5

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

Similar topics

5
by: Brian McClellan | last post by:
Just wondering if anyone has a simple example of creating a gridview completely programmatically, i'm not doing anything terribly sophisticated. When creating the gridview declaratively evertying...
0
by: ASMJ | last post by:
Hi, I have a gridview in my page, which displays all records from an sqldatasource. The delete row command is available to the user, through which he can perform deletion. I want to trap the...
7
by: GaryDean | last post by:
I'm trying to load a GridView up with data manually, in code. I'm not using any datasource. Using this code.... ArrayList myRowArrayList; GridViewRow myGVR = new...
0
by: John Smith | last post by:
ASP.Net 2.0 / C# / IIS 6 I have 2 pages. The master page consists of a tabbed menu created using the Menu and MultiView controls. Something like this: ...
2
by: Loading name... | last post by:
Hey asp.net 2.0 I have a GridView on my webpage. This GridView's datasource is a SqlDataSource. The SqlDataSource returns 3 columns. Here is my problem: My GridView consist of 3 columns...
4
by: ma | last post by:
Hello, I have a GrideView that has 4 col. 3 of them are bound to a dataset and the last one is not. The last one is check box. The check box is defined as follow: <asp:TemplateField...
2
by: DC | last post by:
Hi, I am trying to implement a simple template control (to be used as TemplateItem etc. in GridView). Does someone see why my code fails? I give up for now and try a different approach (http://...
0
by: db007 | last post by:
I have a problem at the moment with a web project. I have two Panels within an UpdatePanel on an aspx page (using Masterpages). I'm using ASP.Net 2.0 with an AJAX enabled website. The two...
2
by: =?Utf-8?B?QXN0cml0aA==?= | last post by:
I am a Newbie and i am scratching my head over how to delete a Row from a GridView. I have this GridView which has 3 fields called ID,ProjCode and ProjDescription of which ID is the hidden...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.