How to Add, Edit, Update Records In asp.net

Total Post:26

Points:182
C# 
C#
 723  View(s)
Ratings:
Rate this:
Hi Guys

I am beginner in c# and currently work one of the project.I am facing a problem to show records in gridview and edit and update records without refersing.Please help me.

I would really appreciate your help.

Thanks


  1. Post:52

    Points:366
    Re: How to show records in gridview

    make sql table taking id as primary key and identity specification yes

    then design html form and add ajax code like this
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test.WebForm1" %>
     
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    <link href="Content/Site.css" rel="stylesheet" />
        <script src="Scripts/jquery-1.7.1.min.js"></script>
       <style>
           span {
               width:300px;
             
           }
           h3 {
               color:blue;
               font-family:Garamond;
           }
       </style>
    </head>
    <body>
       
        <form id="form1" runat="server">

            <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
      <!--if you want that your page should not flikr during record inseting
     updating and deleting then you can add this code and form should be inside update panel-->
        <asp:UpdatePanel ID="panel1" runat="server"><ContentTemplate>
        <div  class="content-wrapper">
            <table>
                <tr>
                    <td>
                        <span id="s1" runat="server"><asp:Label ID="label1" runat="server">User Name</asp:Label></span><br />
                        <asp:TextBox ID="EmpName" runat="server"/>
                         <span id="Span1" runat="server"><asp:Label ID="label2" runat="server">Father Name</asp:Label></span>
                        <asp:TextBox ID="FName" runat="server" />
                    </td>
     
                </tr>
                 <tr>
                    <td>
                        <span id="Span2" runat="server"> <asp:Label ID="label3"  runat="server">Eamil</asp:Label></span><br />
                        <asp:TextBox ID="email" TextMode="Email" runat="server"  />
                        <span id="Span3" runat="server"> <asp:Label ID="label4"  runat="server">Mobile Number</asp:Label></span>
                        <asp:TextBox ID="mobile" TextMode="Number" runat="server" />
                    </td>
     
                </tr>
                <tr>
                    <td>
                <asp:Button ID="submit" runat="server" OnClick="submit_Click1" Text="Submit"/><asp:Label ID="id" runat="server" Visible="false"></asp:Label>
       <asp:Button ID="update" runat="server" OnClick="update_Click" Text="Update" Visible="false"/>
    <asp:Label ID="message" runat="server"></asp:Label>
                    </td>
                    </tr>
            </table>
            <hr />
            <h3><asp:Label ID="Employe" runat="server" Text="Employee Detail"></asp:Label></h3>
            <asp:GridView ID="grid1" DataKeyNames="Id" runat="server" AutoGenerateColumns="false" Width="1024" OnRowDeleting="grid1_RowDeleting" OnRowUpdating="grid1_RowUpdating">
             
                 <Columns>
                 
                     <asp:TemplateField HeaderText="Name">
                         <ItemTemplate>
                             <asp:Label ID="s1" runat="server" Text='<%#Eval("UserName") %>'></asp:Label>
                         </ItemTemplate>
                     </asp:TemplateField>
                      <asp:TemplateField HeaderText="Father Name">
                         <ItemTemplate>
                             <asp:Label ID="s2" runat="server" Text='<%#Eval("FName")%>'></asp:Label>
                         </ItemTemplate>
                     </asp:TemplateField>
                      <asp:TemplateField HeaderText="Email">
                         <ItemTemplate>
                             <asp:Label ID="s3" runat="server" Text='<%#Eval("Email")%>'></asp:Label>
                         </ItemTemplate>
                     </asp:TemplateField>
                      <asp:TemplateField HeaderText="Mobile">
                         <ItemTemplate>
                             <asp:Label ID="s4" runat="server" Text='<%#Eval("Mobile")%>'></asp:Label>
                         </ItemTemplate>
                     </asp:TemplateField>
                  
           
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="b1" runat="server" Text="Delete" CommandName="Delete" CausesValidation="false" />
                          
                        </ItemTemplate>
                    </asp:TemplateField>
                      <asp:TemplateField>
                        <ItemTemplate>
                          
                            <asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Update" CausesValidation="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
                
        </div>
            </ContentTemplate>
            </asp:UpdatePanel>
        </form>
    </body>
    </html>
     

    for bind grid use this code
    void bind()
            {
                DataSet ds = new DataSet();
                using (SqlConnection con = new SqlConnection(@"Data Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial Catalog = testdata"))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select * from EmpDetail", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    con.Close();
                    grid1.DataSource = ds;
                    grid1.DataBind();
                }
            }
    For saving records in database
    
    
    
      protected void submit_Click1(object sender, EventArgs e)
            {
                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = @"Data
    Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
    Catalog = testdata;"
    Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
    Catalog = testdata;";             cnn.Open();               SqlCommand cmd = new SqlCommand();             cmd.CommandText = "insert into EmpDetail values('"
    EmpDetail values('"
    + EmpName.Text + "','"
    + FName.Text + "','"
    + email.Text + "','"
    + mobile.Text + "')";             cmd.Connection = cnn;             int i = cmd.ExecuteNonQuery();             if (i == 1)                 message.Text = "Saved Succesfully!!"
    Succesfully!!";             else                 message.Text = "Error in saveing!!! try again!!"
    saveing!!! try again!!";                           bind();           }

    for deleting and eding record throuh grid view
    
    
    
      protected void grid1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
     
                try
                {
                    string Id =
    grid1.DataKeys[e.RowIndex].Values[
    grid1.DataKeys[e.RowIndex].Values["Id"].ToString();                 SqlConnection cnn = new SqlConnection();                 cnn.ConnectionString = @"Data Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial Catalog = testdata;"
    Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
    Catalog = testdata;";                 cnn.Open();                   SqlCommand cmd = new SqlCommand();                 cmd.CommandText="delete from EmpDetail where Id='"
    EmpDetail where Id='"+Id+"' ";                 cmd.Connection = cnn;                   int i = cmd.ExecuteNonQuery();
    cmd.ExecuteNonQuery();                 if (i == 1)                 {                     bind();                     message.Text = "Record deleted"
    deleted";                 }                 else                 {                     message.Text = "error";                 }               }             catch (Exception ex)             {                 message.Text =
    ex.Message.ToString();             }           }  <--for clearing textboxes after submitting records-->         protected void ClearInputs(ControlCollection ctrls)         {             foreach (Control ctrl in ctrls)             {                 if (ctrl is TextBox)                                     ((TextBox)ctrl).Text = string.Empty;                 ClearInputs(ctrl.Controls);                             }         }                        protected void grid1_RowUpdating(object sender, GridViewUpdateEventArgs e)         {             submit.Visible = false;             update.Visible = true;             string Id = grid1.DataKeys[e.RowIndex].Values["Id"].ToString();             SqlConnection cnn = new SqlConnection();             cnn.ConnectionString = @"Data Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial Catalog = testdata;"
    Source=MSCLIENT-003\MSSQLSERVER2014;User id=sa;Password=mindstick;Initial
    Catalog = testdata;";             cnn.Open();             SqlCommand cmd = new SqlCommand("select * from EmpDetail where Id="
    from EmpDetail where Id=" + Id, cnn);                         SqlDataReader dr = cmd.ExecuteReader();
    cmd.ExecuteReader();             if (dr.HasRows == true)             {                 dr.Read();                 id.Text = dr["Id"].ToString();                 EmpName.Text = dr["UserName"].ToString();                 FName.Text = dr["FName"].ToString();                 email.Text = dr["Email"].ToString();                 mobile.Text = dr["Mobile"].ToString();                   dr.Close();                 cnn.Close();             }         }    

      Modified On Jan-03-2017 02:27:48 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!