473,396 Members | 1,989 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.

How to update database without using SqlDataSource

If I want to access the method in which we define update query, how can I access that method on aspx page & update the data in a grid view without direct use of an query on aspx page under sql data source?
May 17 '10 #1
28 6666
Frinavale
9,735 Expert Mod 8TB
What you would have to do is implement a method that handles the GridView.RowUpdating Event. In this method you will have to retrieve the data from the row that is updating and use this data to update your database.

I'm not sure what you're stuck on so if you provide more information I can give you more help on the topic.

-Frinny
May 17 '10 #2
@Frinavale
sorry frinny for my titile i use......
Actually I made a data acces layer class, buiness logic layer class & presentation layer(.aspx page). Now I want to update the data in a grid view.
In most of the books generally direct update query is written on aspx page under sql data source using update commnad property & do updation in grid view.
Here is the code
Expand|Select|Wrap|Line Numbers
  1. <asp:SqlDataSource ID="datasource1" ProviderName="System.Data.SqlClient"  runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString%>" SelectCommand="Select * From ManageClient" 
  2.  UpdateCommand="Update ManageClient Set ClientName=@ClientName,Address=@Address,MobileNo=@MobileNo,Image=@Image Where ClientId=@ClientId"></asp:SqlDataSource>
  3.  
Connect it to the grid view.

Now i do not want to use update command or connection string in sql data source.
This thing I have done in data acces layer class where I make a method of an update.
I want to use this method so I can do an update in grid view directly.
May 18 '10 #3
Frinavale
9,735 Expert Mod 8TB
I've never actually used the SqlDataSource before. I like to be in more control of my database connections.

Anyways, it's not that hard to do. Delete your SqlDataSource...you don't need it. You don't need to use a SqlDataSource at all. GridViews can bind to DataTables, DataViews, Arrays of Objects ...and more.

So, you need to populate the object that you are going to use as your data source at some point in your page life cycle. You could do this in your Page Load event if you want to.

You could cache the object that you are going to use as a data source too. This way you don't have to repopulate the data source every page postback. This saves time but it depends on how long you can tolerate "old data" being displayed.

Anyways, once you've populated your data source (or retrieved it from cache) you set the GridView.DataSource property to the object you are using...then call the GridView.DataBind method to bind the GridView to the data source. I do this in the Page PreRender event for a bunch of reasons....for example if you bind in the Page Load event every page request you will have problems with getting the values that the user entered while editing (because these values will be over written with the data that you've bound to).

In your GridViews OnRowUpdating event you will call your data access layer's methods to do the updating....

That's pretty much it.

-Frinny
May 18 '10 #4
@Frinavale
You said that we have to bind object with grid view at page.prerender . I think if we do the same thing on page load event it work because value is set when row updating event is fired. When this event fired it saves the values at their view state & this view state is sent to the client. That is the concept of page life cycle. Am I right?
May 19 '10 #5
Frinavale
9,735 Expert Mod 8TB
Not quite right.
What happens is that the ViewState for the GridView is loaded just before the Page Load event. This means that all of the data that the user provided for the row that they are updating is loaded into the ASP.NET controls used to access the data in your C#/VB.NET code.

If you do a DataBind in the Page Load event then all of this data is lost!
Edit: if you do a DataBind in the Page Load event every page request all of your data will be lost

It's the most common problem that people have with GridViews that I've noticed.

If you don't need to get data from the user, than yes you can do the DataBind in the Page Load event. But if you need to get data from the user for updating purposes then, no, you should do it in the Page PreRender event instead.

-Frinny
May 19 '10 #6
@Frinavale
The event is fired between load view state & save view state when the event is fired it set the values in textbox or whatever server control we used. After that it saves the values in view state. Then there is no matter of data loss if you load the page again but I have to bind data in a grid view through view state on page load no chance of data loss.
May 19 '10 #7
Frinavale
9,735 Expert Mod 8TB
You are mistaken.

Events are created when the ViewState is loaded. Methods that handle events occur after the Page Load event.

This is the basic outline of what happens in the ASP.NET Life Cycle:
  • Page Init (before ViewState is loaded)
  • Page Load (after ViewState is loaded)
  • Event handling code is executed
  • Page PreRender (occurs Just before the page is rendered as HTML)

Please review the ASP.NET Page Life Cycle for a more detailed explination.

If you perform a GridView.DataBind in the Page Load event (without checking if it's postback) then you will not be able to retrieve user input in the GridView.RowEditing event.

-Frinny
May 19 '10 #8
@Frinavale
Can you tell me when the data is saved in a view state & that data is loss from the view state next time when it load the view state?
May 19 '10 #9
Frinavale
9,735 Expert Mod 8TB
The ViewState is stored some time in between the Page PreRender event and the SaveStateComplete event (heading towards the Render stage).

All of this is covered in the article I sent you a link to :)

I'm not sure what you mean by:
AnagJohari: that data is loss from the view state
Maybe you're talking about data loss that happens if you don't call the DataBind method??
May 19 '10 #10
@Frinavale
Now i implemented the editing in gridview i handle events & after that i call display method in which i bind the grid view on page load event its work correctly.......
i want to say one more thing i call the display method at the end of every event which i handled ....
like onrowupdating event after performing all operation
i call display method at the end.
you also check this link please , by thw way ur link is not working.
http://msdn.microsoft.com/en-us/library/ms972976.aspx
May 20 '10 #11
Frinavale
9,735 Expert Mod 8TB
I've fixed the link.

I'm sorry AnagJohari, but you've completely lost me.

I'm am fully aware of when ViewState is loaded....it's loaded right after the Page Init Event and before Page Load event.

So, the ViewState with all of the data for your GridView is loaded just after the Init event. This makes the data in your GridView is available in the Page Load event...including data entered by the user.

If you do a DataBind in your Page Load event every page request (without checking if isPostback is False) then you will be overwritting anything that the user provided.


BUT this information that I'm telling you does not matter to you because you don't care about anything the user has entered into the GridView for editing. (Well it should matter to you because this is a very important behaviour that you need to understand)

All you want to do is determine which row the user was editing (which is easily done in the OnRowEditing event)...and retrieve any information that you need in order to display the information in more detail in some other "page" or "control".

You were not very clear about this.
This is why you are able to do a DataBind in your Page Load without losing data entered by the user.

Anyways.
I don't know what you're talking about with regards to your "display" method.
I'm not even sure you have a question at this point... :)

-Frinny
May 20 '10 #12
@Frinavale
Frinny I send You a code ,
By the way You are so senior & i respect each & every word which you are writing for me....
I am just new ,so i m eager to know what is the problem actually .In mycode,
why i donot lose my data in grid view.
I just need your help Thats it

Expand|Select|Wrap|Line Numbers
  1. That is .cs code file
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Web;
  9. using System.Web.Security;
  10. using System.Web.UI;
  11. using System.Web.UI.HtmlControls;
  12. using System.Web.UI.WebControls;
  13. using System.Web.UI.WebControls.WebParts;
  14. using System.Xml.Linq;
  15. using System.Collections.Generic;
  16. using BllDummy;
  17.  
  18. public partial class ManageClient : System.Web.UI.Page
  19. {
  20.     DummyBLL dummy = new DummyBLL();
  21.     protected void Page_Load(object sender, EventArgs e)
  22.     {
  23.         if (!Page.IsPostBack)
  24.         {
  25.             ddlbind();
  26.             display();
  27.         }
  28.     }
  29.     protected void click_Add(object sender, EventArgs e)
  30.     {
  31.         dummy.ClientName = txtClientName.Text;
  32.         dummy.Address = txtAddress.Text;
  33.         dummy.MobileNo = Convert.ToInt64(txtMobileNo.Text);
  34.         dummy.Image = txtImage.Text;
  35.         dummy.add();
  36.         txtAddress.Text = string.Empty;
  37.         txtClientName.Text = string.Empty;
  38.         txtImage.Text = string.Empty;
  39.         txtMobileNo.Text = string.Empty;
  40.         txtClientName.Text = "the addtion is done";
  41.         display();
  42.  
  43.     }
  44.     private void display()
  45.     {
  46.         gv_DisplayClient.DataSource = DummyBLL.display();
  47.        // gv_DisplayClient.DataSourceID = "datasource1";
  48.         gv_DisplayClient.DataBind();
  49.     }
  50.     private void ddlbind()
  51.     {
  52.         List<DummyBLL> list = DummyBLL.Clientlist();
  53.         ddlClient.Items.Add(new ListItem("--SelectClient--", "0"));
  54.         foreach (DummyBLL dummylist in list)
  55.         {
  56.             ddlClient.Items.Add(new ListItem(dummylist.ClientName, dummylist.ClientId.ToString()));
  57.         }
  58.     }
  59.  
  60.  
  61.  
  62.  /*protected void ClickStatus(object sender, GridViewCommandEventArgs e)
  63.     {
  64.         if (e.CommandName == "StatusClick")
  65.         {
  66.             message.Text = "You Currently Click The ClientId=" + e.CommandArgument.ToString();
  67.         }
  68.     }*/
  69.  
  70.     protected void View_Click(object sender, EventArgs e)
  71.     {
  72.  
  73.     }
  74.     protected void GvEditing(object sender, GridViewEditEventArgs e)
  75.     {
  76.         gv_DisplayClient.EditIndex = e.NewEditIndex;
  77.         display();
  78.       }
  79.     protected void gv_EditCancel(object sender, GridViewCancelEditEventArgs e)
  80.     {
  81.         gv_DisplayClient.EditIndex = -1;
  82.         display();
  83.     }
  84.  
  85.     protected void gv_Updating(object sender, GridViewUpdateEventArgs e)
  86.     {
  87.  
  88.         Label lb = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6"); 
  89.         int ClientId = Convert.ToInt32(lb.Text);
  90.         TextBox tx1 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvClientName");
  91.         TextBox tx2 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvAddress");
  92.         TextBox tx3 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvMobileNo");
  93.         TextBox tx4 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvImage");
  94.         dummy.ClientName = tx1.Text;
  95.         dummy.Address = tx2.Text;
  96.         dummy.MobileNo = Convert.ToInt32(tx3.Text);
  97.         dummy.Image = tx4.Text;
  98.         dummy.ClientId = ClientId;
  99.         dummy.Update();
  100.         gv_DisplayClient.EditIndex = -1;
  101.         display();
  102.  
  103.     }
  104.  protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
  105.     {
  106.         Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
  107.         dummy.ClientId = Convert.ToInt32(lbl.Text);
  108.         dummy.Delete();
  109.         display();
  110.     }
  111. this event i write after our discussion so its seprate thing, by the way thisevent is not working
  112. at this time
  113.  protected void Gv_Insert(object sender, GridViewRowEventArgs e)
  114.     {
  115.         TextBox tx1 = (TextBox)gv_DisplayClient.FindControl("gvClientName");
  116.         TextBox tx2 = (TextBox)gv_DisplayClient.FindControl("gvAddress");
  117.         TextBox tx3 = (TextBox)gv_DisplayClient.FindControl("gvMobileNo");
  118.         TextBox tx4 = (TextBox)gv_DisplayClient.FindControl("gvImage");
  119.  
  120.         dummy.ClientName = tx1.Text;
  121.         dummy.Address = tx2.Text;
  122.         dummy.MobileNo = Convert.ToInt32(tx3.Text);
  123.         dummy.Image = tx4.Text;
  124.         dummy.add();
  125.         display();
  126.     }
  127. }
  128.  
  129.  There is problem in this event that is gv_insert i make it in bold letter.
  130. i took on rowcreating event but not able to fetch the value from grid view row
  131. i want that when i click on add the dynamic row is created in editable form
  132. after i enter the value in that row the data is add in database
  133.  
.aspx code
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ManageClient.aspx.cs" Inherits="ManageClient" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.     <table border="3" cellspacing="3" width="300px">
  13.     <tr>
  14.     <td>
  15.     <b>Client_Name</b>
  16.     </td>
  17.     <td>
  18.     <asp:TextBox ID="txtClientName" runat="server" ></asp:TextBox>
  19.   </td>
  20.  
  21.     </tr>
  22.     <tr>
  23.     <td>
  24.     <b>Address</b>
  25.  
  26.  
  27.  
  28.  
  29.     </td>
  30.     <td>
  31.     <asp:TextBox ID="txtAddress" runat="server" ></asp:TextBox>
  32.     </td>
  33.  
  34.     </tr>
  35.     <tr>
  36.     <td>
  37.     <b>Mobile_No</b>
  38.     </td>
  39.     <td>
  40.     <asp:TextBox ID="txtMobileNo" runat="server" ></asp:TextBox>
  41.     </td>
  42.  </tr>
  43.  <tr>
  44.     <td>
  45.     <b>Image</b>
  46.     </td>
  47.     <td>
  48.     <asp:TextBox ID="txtImage" runat="server" ></asp:TextBox>
  49.     </td>
  50.     </tr>
  51.     <tr><td><b>ClientList</b>
  52.     </td>
  53.     <td><asp:DropDownList ID="ddlClient" runat="server"></asp:DropDownList>
  54.     </td></tr>
  55.  
  56.     <tr><td align="right" colspan="2">
  57.     <asp:Button ID="txtAdd" runat="server" Font-Bold="true" OnClick="click_Add" Font-Size="Larger" Text="Add" Height="30px" />
  58.  
  59.    </td>
  60.    </tr>
  61.  
  62.  </table>
  63.  <asp:Button ID="SaveChanges" runat="server" />
  64.  <asp:Button ID="View" runat="server" onclick="View_Click" />
  65.   <asp:GridView ID="gv_DisplayClient" runat="server" DataKeyNames="ClientId" 
  66.             ForeColor="#333333" GridLines="Horizontal"  
  67.             CellPadding="4" AutoGenerateColumns="false" 
  68.               OnRowEditing="GvEditing" OnRowCancelingEdit="gv_EditCancel" 
  69.                 EnableViewState="true" onrowupdating="gv_Updating" 
  70.                 onrowdeleting="gv_Deleting" onrowcreated="Gv_Insert" 
  71.                 >
  72.             <%-- (placed in grid view when using sql data source) OnRowCommand="ClickStatus" DataSourceID="datasource1"--%>
  73.  
  74.  <RowStyle BackColor="#F7F6F3" ForeColor="#000000"/>
  75.      <HeaderStyle BackColor="BurlyWood" ForeColor="AliceBlue" Font-Bold="true" />
  76.  
  77.   <Columns>
  78.   <%-- <asp:BoundField DataField="ClientId" ReadOnly="true" HeaderText="ClientId" />
  79.   <asp:BoundField DataField="ClientName" HeaderText="Client Name" />
  80.   <asp:BoundField DataField="Address" HeaderText="Address" />
  81.   <asp:BoundField DataField="MobileNo" HeaderText="MobileNo" />
  82.   <asp:ImageField DataImageUrlField="Image" HeaderText="Image"/>
  83.    <asp:CommandField ShowEditButton="true"  SelectText="Edit"/>
  84.   <asp:TemplateField HeaderText="click">
  85.   <ItemTemplate>
  86.   <asp:Button ID="click" runat="server" CommandName="StatusClick" CommandArgument='<%#Eval("ClientId")%>' Text="Click" />
  87.  
  88.   </ItemTemplate>
  89.  
  90.   </asp:TemplateField>--%>
  91.   <asp:TemplateField HeaderText="ClientId">
  92.   <ItemTemplate >
  93.   <asp:Label ID="Label6" runat="server" Text='<%#Bind("ClientId")%>'></asp:Label>
  94.   </ItemTemplate>
  95.  
  96.   </asp:TemplateField>
  97.   <asp:TemplateField HeaderText="ClientName">
  98.   <ItemTemplate >
  99.   <asp:Label ID="lblClientName" runat="server" Text='<%#Eval("ClientName")%>'></asp:Label>
  100.   </ItemTemplate>
  101.   <EditItemTemplate>
  102.   <asp:TextBox ID="gvClientName" runat="server" Text='<%#Bind("ClientName")%>'></asp:TextBox>
  103.   </EditItemTemplate>
  104.  
  105.   </asp:TemplateField>
  106.    <asp:TemplateField HeaderText="Address">
  107.   <ItemTemplate >
  108.   <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address")%>'></asp:Label>
  109.   </ItemTemplate>
  110.   <EditItemTemplate>
  111.   <asp:TextBox ID="gvAddress" runat="server" Text='<%#Bind("Address")%>'></asp:TextBox>
  112.   </EditItemTemplate>
  113.  
  114.   </asp:TemplateField>
  115.   <asp:TemplateField HeaderText="MobileNo">
  116.   <ItemTemplate >
  117.   <asp:Label ID="lblMobileNo" runat="server" Text='<%#Eval("MobileNo")%>'></asp:Label>
  118.   </ItemTemplate>
  119.   <EditItemTemplate>
  120.   <asp:TextBox ID="gvMobileNo" runat="server" Text='<%#Bind("MobileNo")%>'></asp:TextBox>
  121.   </EditItemTemplate>
  122.  
  123.   </asp:TemplateField>
  124.   <asp:TemplateField HeaderText="Image">
  125.   <ItemTemplate >
  126.   <asp:Label ID="lblImage" runat="server" Text='<%#Eval("Image")%>'></asp:Label>
  127.   </ItemTemplate>
  128.   <EditItemTemplate>
  129.   <asp:TextBox ID="gvImage" runat="server" Text='<%#Bind("Image")%>'></asp:TextBox>
  130.   </EditItemTemplate>
  131.  
  132.   </asp:TemplateField>
  133.   <asp:TemplateField HeaderText="Operations">
  134.   <ItemTemplate>
  135.   <asp:LinkButton ID="edit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
  136.   <asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
  137.   </ItemTemplate>
  138.   <EditItemTemplate>
  139.                    <asp:LinkButton ID="btnupdate" runat="server" 
  140.             CommandName="Update" Text="Update" ></asp:LinkButton>
  141.                    <asp:LinkButton ID="btncancel" runat="server" 
  142.             CommandName="Cancel" Text="Cancel"></asp:LinkButton>
  143.             <asp:LinkButton ID="btnAdd" runat="server" CommandName="Insert" Text="Insert"></asp:LinkButton>
  144.                </EditItemTemplate>
  145.  </asp:TemplateField>
  146.  </Columns>
  147.  
  148.      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  149.      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  150.      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  151.      <EditRowStyle BackColor="#999999" />
  152.      <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  153.  </asp:GridView>
  154.  
  155.  
  156.  <%-- <asp:SqlDataSource ID="datasource1" ProviderName="System.Data.SqlClient"  runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString%>" SelectCommand="Select * From ManageClient" 
  157.  UpdateCommand="Update ManageClient Set ClientName=@ClientName,Address=@Address,MobileNo=@MobileNo,Image=@Image Where ClientId=@ClientId"></asp:SqlDataSource> --%>
  158.     </div>
  159.     <asp:Label ID="message" runat="server"></asp:Label>
  160.     </form>
  161. </body>
  162. </html>
  163.  
  164.  
please see my code & give your important opinion.
Thank you
May 21 '10 #13
Frinavale
9,735 Expert Mod 8TB
This is a lot easier to understand!

Ok the reason why you aren't losing data is because you are only doing your DataBind in your Page Load event the first time the page is loaded (if !IsPostback). This way your data is not getting over written when the user-provided-edit-data is posted back to the server.

But do you notice how you are calling the display() method in several different places? Wouldn't it be easier if you just had to call it once? That is why I recommended that you do this in your Page PreRender event.

Ok so everything is looking pretty good :)
But instead of using the Insert event to try and populate your TextBoxes, try using the GridView.RowDataBound event :)
Edit: Do not use the DataRowBound event....you should be using the GridView.RowCommand event. I should have been more observant earlier to see what you were trying to do! I'm sorry.




-Frinny
May 21 '10 #14
@Frinavale
Now I get the answer your mean is that when we bind the data at page load event. the view state is already loaded with previous value & that value again saved in a view state so the same data is displayed again & again whether you edit the data in a grid view or not.
but if we bind the data at page.prerender event then this value is loaded in a view state & after page load event this view state is saved & the data is displayed with updated value..
Am i right frinny? please reply me that what i said is right or not.
other thing i want to know what about the load postback event that is also fire between pageprerender & page load . i read about this event in msdn but not able to distinquish beacuse loadpostback event is also use to remember the value & pageprerender also for remebering the values.
thank you
May 21 '10 #15
Frinavale
9,735 Expert Mod 8TB
Now I get the anser your mean is that when we bind the data at page load event. the view state is already loaded with previous value & that value gain saved in a view state so the same data is displayed again & again whether you edit the data in a grid view or not.
:)

but if we bind the data at page.prerender event then this value is loaded in a view state & after page load event this view state is saved & the data is displayed with updated value..
Am i right frinny? please reply me that what i said is right or not.
The point I was trying to make is that you should be binding to your GridView after you have finished manipulating the data within it.....after the row had been deleted or edited or added. The PreRender event occurs after everything is finished executing and is sometimes a good place to do this.

You are doing things just fine the way you are.

I was recommending doing it in the PreRender event for simplicity sake but after playing around with a your GridView and my own little custom DummyBLL class...I am starting to think that your current technique is better than my suggestion!

:)


other thing i want to know what about the load postback event that is also fire between pageprerender & page load . i read about this event in msdn but not able to distinquish beacuse loadpostback event is also use to remember the value & pageprerender also for remebering the values.
Which article were you reading?


One thing I noticed when I took your took your GridView (that you posted) is that your DummyBLL class has a property "ClientID".

"ClientID" has a special meaning in ASP.NET controls...it is the ID that is given to the HTML element that represents the control in the browser.

You should consider renaming this property to avoid confusion.

This is what I used while testing your code:

GridView (ASPX)...notice how I changed the ClientID to DummyID:
Expand|Select|Wrap|Line Numbers
  1.     <asp:GridView ID="gv_DisplayClient" runat="server" DataKeyNames="DummyID" ForeColor="#333333"
  2.         GridLines="Horizontal" CellPadding="4" AutoGenerateColumns="false" OnRowEditing="GvEditing"
  3.         OnRowCancelingEdit="gv_EditCancel" EnableViewState="true" OnRowUpdating="gv_Updating"
  4.         OnRowDeleting="gv_Deleting">
  5.         <RowStyle BackColor="#F7F6F3" ForeColor="#000000" />
  6.         <HeaderStyle BackColor="BurlyWood" ForeColor="AliceBlue" Font-Bold="true" />
  7.         <Columns>
  8.             <%-- <asp:BoundField DataField="ClientId" ReadOnly="true" HeaderText="ClientId" />
  9.   <asp:BoundField DataField="ClientName" HeaderText="Client Name" />
  10.   <asp:BoundField DataField="Address" HeaderText="Address" />
  11.   <asp:BoundField DataField="MobileNo" HeaderText="MobileNo" />
  12.   <asp:ImageField DataImageUrlField="Image" HeaderText="Image"/>
  13.    <asp:CommandField ShowEditButton="true"  SelectText="Edit"/>
  14.   <asp:TemplateField HeaderText="click">
  15.   <ItemTemplate>
  16.   <asp:Button ID="click" runat="server" CommandName="StatusClick" CommandArgument='<%#Eval("ClientId")%>' Text="Click" />
  17.  
  18.   </ItemTemplate>
  19.  
  20.   </asp:TemplateField>--%>
  21.             <asp:TemplateField HeaderText="ClientId">
  22.                 <ItemTemplate>
  23.                     <asp:Label ID="Label6" runat="server" Text='<%#Bind("DummyID")%>'></asp:Label>
  24.                 </ItemTemplate>
  25.             </asp:TemplateField>
  26.             <asp:TemplateField HeaderText="ClientName">
  27.                 <ItemTemplate>
  28.                     <asp:Label ID="lblClientName" runat="server" Text='<%#Eval("ClientName")%>'></asp:Label>
  29.                 </ItemTemplate>
  30.                 <EditItemTemplate>
  31.                     <asp:TextBox ID="gvClientName" runat="server" Text='<%#Bind("ClientName")%>'></asp:TextBox>
  32.                 </EditItemTemplate>
  33.             </asp:TemplateField>
  34.             <asp:TemplateField HeaderText="Address">
  35.                 <ItemTemplate>
  36.                     <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address")%>'></asp:Label>
  37.                 </ItemTemplate>
  38.                 <EditItemTemplate>
  39.                     <asp:TextBox ID="gvAddress" runat="server" Text='<%#Bind("Address")%>'></asp:TextBox>
  40.                 </EditItemTemplate>
  41.             </asp:TemplateField>
  42.             <asp:TemplateField HeaderText="MobileNo">
  43.                 <ItemTemplate>
  44.                     <asp:Label ID="lblMobileNo" runat="server" Text='<%#Eval("MobileNo")%>'></asp:Label>
  45.                 </ItemTemplate>
  46.                 <EditItemTemplate>
  47.                     <asp:TextBox ID="gvMobileNo" runat="server" Text='<%#Bind("MobileNo")%>'></asp:TextBox>
  48.                 </EditItemTemplate>
  49.             </asp:TemplateField>
  50.             <asp:TemplateField HeaderText="Image">
  51.                 <ItemTemplate>
  52.                     <asp:Label ID="lblImage" runat="server" Text='<%#Eval("Image")%>'></asp:Label>
  53.                 </ItemTemplate>
  54.                 <EditItemTemplate>
  55.                     <asp:TextBox ID="gvImage" runat="server" Text='<%#Bind("Image")%>'></asp:TextBox>
  56.                 </EditItemTemplate>
  57.             </asp:TemplateField>
  58.             <asp:TemplateField HeaderText="Operations">
  59.                 <ItemTemplate>
  60.                     <asp:LinkButton ID="edit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
  61.                     <asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
  62.                 </ItemTemplate>
  63.                 <EditItemTemplate>
  64.                     <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
  65.                     <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
  66.                     <asp:LinkButton ID="btnAdd" runat="server" CommandName="Insert" Text="Insert"></asp:LinkButton>
  67.                 </EditItemTemplate>
  68.             </asp:TemplateField>
  69.         </Columns>
  70.         <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  71.         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  72.         <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  73.         <EditRowStyle BackColor="#999999" />
  74.         <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  75.     </asp:GridView>
C#:
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.       if (IsPostBack == false)
  4.       {
  5.           gv_DisplayClient.DataSource = DummyBLL.display();
  6.           gv_DisplayClient.DataBind();
  7.       }
  8. }
  9.  
  10.  
  11. protected void GvEditing(object sender, GridViewEditEventArgs e)
  12. {
  13.     gv_DisplayClient.EditIndex = e.NewEditIndex;
  14.     display();
  15. }
  16.  
  17. protected void gv_EditCancel(object sender, GridViewCancelEditEventArgs e)
  18. {
  19.     gv_DisplayClient.EditIndex = -1;
  20.     display();
  21. }
  22.  
  23.  
  24. protected void gv_Updating(object sender, GridViewUpdateEventArgs e)
  25. {
  26.  
  27.     Label lb = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
  28.     int ClientId = Convert.ToInt32(lb.Text);
  29.     TextBox tx1 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvClientName");
  30.     TextBox tx2 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvAddress");
  31.     TextBox tx3 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvMobileNo");
  32.     TextBox tx4 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvImage");
  33.     DummyBLL dummy = new DummyBLL();
  34.     dummy.ClientName = tx1.Text;
  35.     dummy.Address = tx2.Text;
  36.     dummy.MobileNo = tx3.Text;
  37.     dummy.Image = tx4.Text;
  38.     dummy.DummyID = ClientId;
  39.     DummyBLL.update(dummy);
  40.     gv_DisplayClient.EditIndex = -1;
  41.     display();
  42.  
  43. }
  44.  
  45. protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
  46. {
  47.     Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
  48.  
  49.     Int32 dummyID = Convert.ToInt32(lbl.Text);
  50.     DummyBLL.delete(dummyID);
  51.     display();
  52. }
  53.  
  54. private void display() {
  55.     gv_DisplayClient.DataSource = DummyBLL.display();
  56.     gv_DisplayClient.DataBind();
  57. }
And this is my custom DummyBLL class (Please note that I don't have a database to connect to so I'm using a List<DummyBLL> which is stored in Session instead.):
Expand|Select|Wrap|Line Numbers
  1.  
  2. private class DummyBLL
  3. {
  4.     private static List<DummyBLL> _source;
  5.     private Int32 _id;
  6.     private String _name;
  7.     private String _address;
  8.     private String _mobileNo;
  9.     private String _image;
  10.  
  11.     public Int32 DummyID
  12.     {
  13.         get { return _id; }
  14.         set { _id = value; }
  15.     }
  16.     public String ClientName
  17.     {
  18.         get { return _name; }
  19.         set { _name = value; }
  20.     }
  21.     public String Address
  22.     {
  23.         get { return _address; }
  24.         set { _address = value; }
  25.     }
  26.     public String MobileNo
  27.     {
  28.         get { return _mobileNo; }
  29.         set { _mobileNo = value; }
  30.     }
  31.     public String Image
  32.     {
  33.         get { return _image; }
  34.         set { _image = value; }
  35.     }
  36.  
  37.     public static List<DummyBLL> display()
  38.     {
  39.         List<DummyBLL> source = new List<DummyBLL>();
  40.         if (HttpContext.Current.Session["_source"] != null)
  41.         {
  42.             _source = (List<DummyBLL>)HttpContext.Current.Session["_source"];
  43.             source = _source;
  44.         }
  45.         else
  46.         {
  47.             String[] names = { "Bob", "Sally", "Frank", "Steve", "Beth" };
  48.  
  49.             int i = 0;
  50.             foreach (string name in names)
  51.             {
  52.                 source.Add(new DummyBLL(i, name, name + "'s address", "1-999-333-222" + i.ToString(), name + "'s picture"));
  53.                 i++;
  54.             }
  55.         }
  56.         _source = source;
  57.         HttpContext.Current.Session["_source"] = _source;
  58.         return source;
  59.     }
  60.     public DummyBLL()
  61.     {
  62.         _id = 0;
  63.         _name = "";
  64.         _address = "";
  65.         _mobileNo = "";
  66.         _image = "";
  67.     }
  68.     public DummyBLL(Int32 id, String name, String address, String mobileNo, String imgName)
  69.     {
  70.         _id = id;
  71.         _name = name;
  72.         _address = address;
  73.         _mobileNo = mobileNo;
  74.         _image = imgName;
  75.     }
  76.     public static void add(DummyBLL newDummyBLL)
  77.     {
  78.         _source = display();
  79.         newDummyBLL.DummyID = _source.Count + 1;
  80.         _source.Add(newDummyBLL);
  81.         HttpContext.Current.Session["_source"] = _source;
  82.     }
  83.     public static void delete(Int32 id)
  84.     {
  85.         _source = display();
  86.         DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
  87.         if (toDelete != null)
  88.         {
  89.             _source.Remove(toDelete);
  90.         }
  91.         HttpContext.Current.Session["_source"] = _source;
  92.     }
  93.     public static void update(DummyBLL updatedDummy)
  94.     {
  95.         Int32 id = updatedDummy.DummyID;
  96.         _source = display();
  97.         DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
  98.         if (toDelete != null)
  99.         {
  100.             _source.Remove(toDelete);
  101.             _source.Add(updatedDummy);
  102.         }
  103.         HttpContext.Current.Session["_source"] = _source;
  104.     }
  105.  
  106. }
-Frinny
May 21 '10 #16
Frinavale
9,735 Expert Mod 8TB
Now that I have some code to see and work with I think I understand your problem a Lot better now.

You want to be able to let the user insert a row with a new DummyBLL record.
You've added an "insert button" link button into the EditTemplate for each row and you can't figure out how to handle this.

I'm sorry but the RowDataBound event that I recommended earlier will not work. At that point I hadn't take a serious look at your code.

You want to use the GridView.RowCommand Event.

This Event is thrown every time a control within the GridView causes a post back to the server. You can determine which control it was using the GridViewCommandEventArgs object passed in as parameter "e".

So, you should have something like:
Expand|Select|Wrap|Line Numbers
  1. protected void gv_DisplayClient_RowCommand(Object sender, GridViewCommandEventArgs e)
  2. {
  3.     if (e.CommandName == "Insert")
  4.     {
  5.         GridViewRow rowToInsert = gv_DisplayClient.Rows[gv_DisplayClient.EditIndex];
  6.         if (rowToInsert != null)
  7.         {
  8.             TextBox tx1 = (TextBox)rowToInsert.FindControl("gvClientName");
  9.             TextBox tx2 = (TextBox)rowToInsert.FindControl("gvAddress");
  10.             TextBox tx3 = (TextBox)rowToInsert.FindControl("gvMobileNo");
  11.             TextBox tx4 = (TextBox)rowToInsert.FindControl("gvImage");
  12.             DummyBLL dummy = new DummyBLL();
  13.             dummy.ClientName = tx1.Text;
  14.             dummy.Address = tx2.Text;
  15.             dummy.MobileNo = tx3.Text;
  16.             dummy.Image = tx4.Text;
  17.             DummyBLL.add(dummy);
  18.             gv_DisplayClient.EditIndex = -1;
  19.             display();
  20.         }
  21.     }
  22. }
Where you have you have your GridView definition as:
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView 
  2.     ID="gv_DisplayClient" 
  3.     runat="server" 
  4.     DataKeyNames="DummyID" 
  5.     ForeColor="#333333"
  6.     GridLines="Horizontal" 
  7.     CellPadding="4" 
  8.     AutoGenerateColumns="false" 
  9.     OnRowEditing="GvEditing"
  10.     OnRowCancelingEdit="gv_EditCancel" 
  11.     EnableViewState="true" 
  12.     OnRowUpdating="gv_Updating"
  13.     OnRowDeleting="gv_Deleting"
  14.     OnRowCommand="gv_DisplayClient_RowCommand">
I personally think this is confusing
May 21 '10 #17
Frinavale
9,735 Expert Mod 8TB
I personally find it confusing to have to click the Edit button to be able to Insert a new record...especially since when you hit the Edit button the row becomes editable and the Insert takes the values of the row you're "editing" but the row your "editing" is not effected by the insert.

I would put an insert button outside of the GridView.
Or I would add an "empty" row to the GridView...and if the user clicks on this row they will only have Insert available.

You could add this row as a "footer" or a "header"....

There's lots of options really that are less confusing to use than what you currently have.
May 21 '10 #18
Frinavale
9,735 Expert Mod 8TB
Here's an example using the Footer to add a New DummyBLL :)

In your GridView you'll have to add a FooterItem to your ItemTemplates. You also have to set "ShowFooter = 'true'". (I changed the style of the header row and the footer row so that they match and so that the text stands out better by setting the background colour to that nice navy colour you used for your paging row)
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="gv_DisplayClient" runat="server" 
  2.     DataKeyNames="DummyID" ForeColor="#333333" GridLines="Horizontal" CellPadding="4" 
  3.     AutoGenerateColumns="false" 
  4.     OnRowEditing="GvEditing"
  5.     OnRowCancelingEdit="gv_EditCancel" 
  6.     EnableViewState="true" 
  7.     OnRowUpdating="gv_Updating"
  8.     OnRowDeleting="gv_Deleting" 
  9.     OnRowCommand="gv_DisplayClient_RowCommand"
  10.     ShowFooter="true">
  11. <Columns>
  12.     <asp:TemplateField HeaderText="ClientId">
  13.         <ItemTemplate>
  14.             <asp:Label ID="Label6" runat="server" Text='<%#Bind("DummyID")%>'></asp:Label>
  15.         </ItemTemplate>
  16.         <FooterTemplate>
  17.             <asp:Label ID="LblInsertPrompt" runat="server" Text="New Dummy:"></asp:Label>
  18.         </FooterTemplate>
  19.     </asp:TemplateField>
  20.     <asp:TemplateField HeaderText="ClientName">
  21.         <ItemTemplate>
  22.             <asp:Label ID="lblClientName" runat="server" Text='<%#Eval("ClientName")%>'></asp:Label>
  23.         </ItemTemplate>
  24.         <EditItemTemplate>
  25.             <asp:TextBox ID="gvClientName" runat="server" Text='<%#Bind("ClientName")%>'></asp:TextBox>
  26.         </EditItemTemplate>
  27.         <FooterTemplate>
  28.             <asp:TextBox ID="gvClientName" runat="server"></asp:TextBox>
  29.         </FooterTemplate>
  30.     </asp:TemplateField>
  31.     <asp:TemplateField HeaderText="Address">
  32.         <ItemTemplate>
  33.             <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address")%>'></asp:Label>
  34.         </ItemTemplate>
  35.         <EditItemTemplate>
  36.             <asp:TextBox ID="gvAddress" runat="server" Text='<%#Bind("Address")%>'></asp:TextBox>
  37.         </EditItemTemplate>
  38.         <FooterTemplate>
  39.             <asp:TextBox ID="gvAddress" runat="server"></asp:TextBox>
  40.         </FooterTemplate>
  41.     </asp:TemplateField>
  42.     <asp:TemplateField HeaderText="MobileNo">
  43.         <ItemTemplate>
  44.             <asp:Label ID="lblMobileNo" runat="server" Text='<%#Eval("MobileNo")%>'></asp:Label>
  45.         </ItemTemplate>
  46.         <EditItemTemplate>
  47.             <asp:TextBox ID="gvMobileNo" runat="server" Text='<%#Bind("MobileNo")%>'></asp:TextBox>
  48.         </EditItemTemplate>
  49.         <FooterTemplate>
  50.             <asp:TextBox ID="gvMobileNo" runat="server"></asp:TextBox>
  51.         </FooterTemplate>
  52.     </asp:TemplateField>
  53.     <asp:TemplateField HeaderText="Image">
  54.         <ItemTemplate>
  55.             <asp:Label ID="lblImage" runat="server" Text='<%#Eval("Image")%>'></asp:Label>
  56.         </ItemTemplate>
  57.         <EditItemTemplate>
  58.             <asp:TextBox ID="gvImage" runat="server" Text='<%#Bind("Image")%>'></asp:TextBox>
  59.         </EditItemTemplate>
  60.         <FooterTemplate>
  61.             <asp:TextBox ID="gvImage" runat="server" Text='<%#Bind("Image")%>'></asp:TextBox>
  62.         </FooterTemplate>
  63.     </asp:TemplateField>
  64.     <asp:TemplateField HeaderText="Operations">
  65.         <ItemTemplate>
  66.             <asp:LinkButton ID="edit" runat="server" CommandName="Edit" Text="Edit" ForeColor="#284775"></asp:LinkButton>
  67.             <asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete" ForeColor="#284775"></asp:LinkButton>
  68.         </ItemTemplate>
  69.         <EditItemTemplate>
  70.             <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update" ForeColor="#284775"></asp:LinkButton>
  71.             <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel" ForeColor="#284775"></asp:LinkButton>
  72.         </EditItemTemplate>
  73.         <FooterTemplate>
  74.             <asp:LinkButton ID="btnAdd" runat="server" CommandName="Insert" Text="Insert New" ForeColor="White"></asp:LinkButton>
  75.         </FooterTemplate>
  76.     </asp:TemplateField>
  77. </Columns>
  78. <RowStyle BackColor="#F7F6F3" ForeColor="#000000" />
  79. <HeaderStyle BackColor="#284775" ForeColor="AliceBlue" Font-Bold="true" />
  80. <FooterStyle BackColor="#284775" ForeColor="AliceBlue" />
  81. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  82. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  83. <EditRowStyle BackColor="#999999" />
  84. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  85. </asp:GridView>
And in the C# code, for the GridView's RowCommand event I have:
Expand|Select|Wrap|Line Numbers
  1. protected void gv_DisplayClient_RowCommand(Object sender, GridViewCommandEventArgs e)
  2. {
  3.     if (e.CommandName == "Insert")
  4.     {
  5.         GridViewRow rowToInsert = gv_DisplayClient.FooterRow;
  6.         if (rowToInsert != null)
  7.         {
  8.             gv_DisplayClient.EditIndex = -1;
  9.  
  10.             TextBox tx1 = (TextBox)rowToInsert.FindControl("gvClientName");
  11.             TextBox tx2 = (TextBox)rowToInsert.FindControl("gvAddress");
  12.             TextBox tx3 = (TextBox)rowToInsert.FindControl("gvMobileNo");
  13.             TextBox tx4 = (TextBox)rowToInsert.FindControl("gvImage");
  14.             DummyBLL dummy = new DummyBLL();
  15.             dummy.ClientName = tx1.Text;
  16.             dummy.Address = tx2.Text;
  17.             dummy.MobileNo = tx3.Text;
  18.             dummy.Image = tx4.Text;
  19.             DummyBLL.add(dummy);
  20.             display();
  21.         }
  22.     }
  23. }
May 21 '10 #19
@Frinavale
Thank You very much for writing such sort of code & giving me your precious time....
by the way i also get anew thing from your code related to session how we can work if we donot have database ,really thanks
i m not able to understand one statement ....
Expand|Select|Wrap|Line Numbers
  1. DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id); 
  2.  
please explain the above line
its new for me the coding style u adopt. plz explain about this also.
Expand|Select|Wrap|Line Numbers
  1. (x) => x.DummyID == id
  2.  
Thank you again
.
May 22 '10 #20
@AnagJohari
Actually i want to take the advantage of using add button in every row of grid view, i want that when i click on add button the new row is created at the next index on which row i clicked. so it helps to insert any recode at any position on grid view. but i donot want that effect the clientid.
suppose
you click on the add button of that row whose client id 3 and suppose index is also 3
& there are other three records downside
so total records is 6 last record in a grid view have client id 6
i want that the record of client id 7 just insert down the record of client id 3
on which i click for insert
Thank you


its looks more user friendly .
May 22 '10 #21
@AnagJohari
i read this article in which load post back data event is fired .... can u explain the diffrence please load view state & load post back data
This is the link
http://msdn.microsoft.com/en-us/library/ms972976.aspx
May 22 '10 #22
@AnagJohari
I add another thing also is that when i call display & bind function on page load event i have never losed the data . the record i edit or add display properly on grid view while i also donot use display button after every event also
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Collections.Generic;
  14. using BllDummy;
  15.  
  16. public partial class ManageClient : System.Web.UI.Page
  17. {
  18.  
  19.     DummyBLL dummy = new DummyBLL();
  20.     protected void Page_Load(object sender, EventArgs e)
  21.     {
  22.  
  23.         ddlbind();
  24.         display();
  25.  
  26.         if (!Page.IsPostBack)
  27.         {
  28.             // ddlbind();
  29.             //display();
  30.         }
  31.     }
  32.     protected void click_Add(object sender, EventArgs e)
  33.     {
  34.         dummy.ClientName = txtClientName.Text;
  35.         dummy.Address = txtAddress.Text;
  36.         dummy.MobileNo = Convert.ToInt64(txtMobileNo.Text);
  37.         dummy.Image = txtImage.Text;
  38.         dummy.add();
  39.         txtAddress.Text = string.Empty;
  40.         txtClientName.Text = string.Empty;
  41.         txtImage.Text = string.Empty;
  42.         txtMobileNo.Text = string.Empty;
  43.         txtClientName.Text = "the addtion is done";
  44.         display();
  45.  
  46.     }
  47.     private void display()
  48.     {
  49.         gv_DisplayClient.DataSource = DummyBLL.display();
  50.         // gv_DisplayClient.DataSourceID = "datasource1";
  51.         gv_DisplayClient.DataBind();
  52.     }
  53.     private void ddlbind()
  54.     {
  55.         List<DummyBLL> list = DummyBLL.Clientlist();
  56.         ddlClient.Items.Add(new ListItem("--SelectClient--", "0"));
  57.         foreach (DummyBLL dummylist in list)
  58.         {
  59.             ddlClient.Items.Add(new ListItem(dummylist.ClientName, dummylist.ClientId.ToString()));
  60.         }
  61.     }
  62.  
  63.  
  64.  
  65.     /*protected void ClickStatus(object sender, GridViewCommandEventArgs e)
  66.        {
  67.            if (e.CommandName == "StatusClick")
  68.            {
  69.                message.Text = "You Currently Click The ClientId=" + e.CommandArgument.ToString();
  70.            }
  71.        }*/
  72.  
  73.     protected void View_Click(object sender, EventArgs e)
  74.     {
  75.  
  76.     }
  77.     protected void GvEditing(object sender, GridViewEditEventArgs e)
  78.     {
  79.         gv_DisplayClient.EditIndex = e.NewEditIndex;
  80.         display();
  81.     }
  82.     protected void gv_EditCancel(object sender, GridViewCancelEditEventArgs e)
  83.     {
  84.         gv_DisplayClient.EditIndex = -1;
  85.         display();
  86.     }
  87.  
  88.     protected void gv_Updating(object sender, GridViewUpdateEventArgs e)
  89.     {
  90.  
  91.         Label lb = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
  92.         int ClientId = Convert.ToInt32(lb.Text);
  93.         TextBox tx1 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvClientName");
  94.         TextBox tx2 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvAddress");
  95.         TextBox tx3 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvMobileNo");
  96.         TextBox tx4 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvImage");
  97.         dummy.ClientName = tx1.Text;
  98.         dummy.Address = tx2.Text;
  99.         dummy.MobileNo = Convert.ToInt32(tx3.Text);
  100.         dummy.Image = tx4.Text;
  101.         dummy.ClientId = ClientId;
  102.         dummy.Update();
  103.         gv_DisplayClient.EditIndex = -1;
  104.         // display();
  105.  
  106.     }
  107.  
  108.  
  109.     protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
  110.     {
  111.         Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
  112.         dummy.ClientId = Convert.ToInt32(lbl.Text);
  113.         dummy.Delete();
  114.         // display();
  115.     }
  116.  
  117.  
  118.  
  119.  
  120.     /* protected void Gv_Insert(object sender, GridViewRowEventArgs e)
  121.      {
  122.          TextBox tx1 = (TextBox)gv_DisplayClient.FindControl("gvClientName");
  123.          TextBox tx2 = (TextBox)gv_DisplayClient.FindControl("gvAddress");
  124.          TextBox tx3 = (TextBox)gv_DisplayClient.FindControl("gvMobileNo");
  125.          TextBox tx4 = (TextBox)gv_DisplayClient.FindControl("gvImage");
  126.  
  127.          dummy.ClientName = tx1.Text;
  128.          dummy.Address = tx2.Text;
  129.          dummy.MobileNo = Convert.ToInt32(tx3.Text);
  130.          dummy.Image = tx4.Text;
  131.          dummy.add();
  132.         // display();
  133.      }*/
  134.     protected void gv_Add(object sender, GridViewCommandEventArgs e)
  135.     {
  136.         {
  137.             if (e.CommandName == "Insert")
  138.             {
  139.                 GridViewRow row = gv_DisplayClient.FooterRow;
  140.                 if (row != null)
  141.                 {
  142.                     // gv_DisplayClient.EditIndex = -1;
  143.                     TextBox tx1 = (TextBox)row.FindControl("txtaddClientName");
  144.                     TextBox tx2 = (TextBox)row.FindControl("txtaddAddress");
  145.                     TextBox tx3 = (TextBox)row.FindControl("txtaddMobileNo");
  146.                     TextBox tx4 = (TextBox)row.FindControl("txtaddImage");
  147.                     dummy.ClientName = tx1.Text;
  148.                     dummy.Address = tx2.Text;
  149.                     dummy.MobileNo = Convert.ToInt32(tx3.Text);
  150.                     dummy.Image = tx4.Text;
  151.                     dummy.add();
  152.                     // display();
  153.                 }
  154.             }
  155.  
  156.         }
  157.     }
  158.  
  159.     protected void gv_DisplayClient_PreRender(object sender, EventArgs e)
  160.     { 
  161.         //ddlbind();
  162.         //display();
  163.  
  164.  
  165.     }
  166. }
Its work fine while u said that the data is loss but data is not lost.
i am really confuse in this topic.....
May 22 '10 #23
Frinavale
9,735 Expert Mod 8TB
I don't know why you aren't experiencing data loss.

This has been a common problem that is constantly posted about and the fix is to move what you're doing in Page Load (every time the page is loaded) to the Page PreRender event (ever time the page is about to be rendered).

Then again it might have something to do with the fact that you have specifically stated that you are enabling ViewState for the GridView....I wonder if this could be why you aren't having problems.
May 23 '10 #24
Frinavale
9,735 Expert Mod 8TB
This statement:
Expand|Select|Wrap|Line Numbers
  1. DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id); 
Finds the first DummyBLL object in the _source (an Array of DummyBLL objects) that has a "DummyID" property that matches the id passed in as a parameter to the method.

It uses lambda expressions to simplify things.

I remember the first time I saw code like this....it changed my coding style completely.

-Frinny
May 23 '10 #25
@Frinavale
Actually I am mistaken data is actually loss when i call this on page load event
now its clear........
May 23 '10 #26
i want to take the advantage of using add button in every row of grid view, i want that when i click on add button the new row is created at the next index on which row i clicked. so it helps to insert any recode at any position on grid view. but i donot want that effect the clientid.
suppose
you click on the add button of that row whose client id 3 and suppose index is also 3
& there are other three records downside
so total records is 6 last record in a grid view have client id 6
i want that the record of client id 7 just insert down the record of client id 3
on which i click for insert
Thank you


its looks more user friendly .
May 23 '10 #27
Frinavale
9,735 Expert Mod 8TB
Hmm interesting.
I think you will have to modify the datasource itself to allow for this...
I'll have to think more on the topic. I'm pretty busy this weekend though and probably won't be able to help you very quickly on this one....

Try inserting a New DummyBLL into your source at the spot that you want it to be at. Give it an ID and let the user enter the rest of the data before saving it to your database (or whatever data store you're using).

-Frinny
May 23 '10 #28
Frinavale
9,735 Expert Mod 8TB
See this thread for the answer: How to insert a row at any position in a GridView
May 26 '10 #29

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

Similar topics

2
by: Nigi | last post by:
I've made a mysql database with php front end which, in part, contains mailing lists. I've used a mailto: link to create a new email with their names in the bcc: field. Unfortunatley their is a...
3
by: phanimadhav | last post by:
hi this is sudheer, i am new one of this ASP.NET .i have one problem,i am using gridview control i know how to place the dropdownlistbox in gridview control.in my gridveiw control have contain...
0
by: jackvel | last post by:
Hi Dudes... I want to connect the Access database with VB 6.0 without using DataControl and ADODC.. Which option should i use.? Plz send me the detailed procedure of doing the same.. Plz...
3
by: mathewgk80 | last post by:
Hi all, I would like to connect gridview with database without using sql datasource.. I am using sql server and c#.net.. Please help me.. regards, Mathew.
4
by: jaz215 | last post by:
hi! how do i add a record in a database without using the datacontrol and adodc. i want to rework my code to using purely codes and not being dependent on the design on vb. so far i have. Set rs...
10
by: gagonm | last post by:
Hi I m looking for some sample example (Code) for my following scenario I have a Grid View which populates data from database say from Product Tables using dataset and dataadapter This...
30
vikas1111
by: vikas1111 | last post by:
This is the code which i have written..... Option Explicit Dim c As New ADODB.Connection Dim cm As ADODB.Command Dim rs As New ADODB.Recordset Private Sub regno_Change() c.Open...
1
by: agarwalsunitadhn | last post by:
I am creating gridview using connectionstring. i am passing different queries to bind grid as per condition. Now the problem I am facing on editing the grid. I am not able to use boundfield or...
0
by: shankarnag | last post by:
i had dumped mysql database by using mysqldump.exe.If i didnt leave space between tables it is showing mysqldump.exe has stopped working.How can i overcome this problem?
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: 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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.