Create,Read,Update,Delete CRUD Operations using C#,Asp.Net,SQL Server Part 2

Bangalore:Hi friends  Please check the part 1 for create operations Click
Create Table as explained in part 1.
Here we are going to discuss about Update as well as Search operations. so first we will enter a name in username field and on text changing we will search in database that is there any user with this name then it will fill all other details into text field and on submit button click you can do update operations.

So design your update form like this and disable all text fields other than username. Please go through the Update.Aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Update.aspx.cs" Inherits="CRUD.Update" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Update</title>
    <script language="javascript" type="text/javascript">

        function empty() {
            document.getElementById("txtUserName").value = "";
            document.getElementById("txtEmail").value = "";
            document.getElementById("txtCompany").value = "";
            document.getElementById("txtMobile").value = "";
            return true;
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <table width="500px">
    <tr class="width">
    <td nowrap="nowrap">UserName
    </td>
    <td><asp:TextBox ID="txtUserName" runat="server" 
            ontextchanged="txtUserName_TextChanged" AutoPostBack="true"></asp:TextBox></td>

    <td nowrap="nowrap">Email</td>
    <td><asp:TextBox ID="txtEmail" runat="server" Enabled="false"></asp:TextBox> </td>
    </tr>
    <tr class="width">
    
    <td nowrap="nowrap">CompanyName</td>
    <td><asp:TextBox ID="txtCompany" runat="server" Enabled="false"></asp:TextBox></td>
    <td nowrap="nowrap">Mobile</td>
    <td><asp:TextBox ID="txtMobile" runat="server" Enabled="false"></asp:TextBox></td>
    </tr>
    <tr>
    <td colspan="3" align="right"><asp:Button ID="btnSubmit" Text="Update" 
            runat="server" onclick="btnSubmit_Click" /> </td>
    <td><asp:Button ID="btnCancel" Text="Clear" runat="server"   OnClientClick="javascript:empty()"   CausesValidation="false"   /> </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


In code Behind we have one textchanged event and button click event. On text changing we will fill all data's as from db as per the username you have provided. and on update button click it will fire the update query.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace CRUD
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            string uname = txtUserName.Text;
            string email = txtEmail.Text;
            string comapany = txtCompany.Text;
            string Mobile = txtMobile.Text;
            string str = getConstring();
            SqlConnection con = new SqlConnection(str);
            con.Open();

            SqlCommand cmd = new SqlCommand("update  crud set email= '" + email + "',company='"+ comapany + "',mobile='"+Mobile+"'where username='" + uname +  "'", con);
            int result = cmd.ExecuteNonQuery();
            if (result == 1)
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "showAlert", "alert('Data Updated Succesfully')", true);
                txtCompany.Text = "";
                txtEmail.Text = "";
                txtMobile.Text = "";
                txtUserName.Text = "";
                txtEmail.Enabled = false;
                txtMobile.Enabled = false;
                txtCompany.Enabled = false;
                txtUserName.Enabled = true;
            }

        }
        public string getConstring()
        {
            string constr = ConfigurationManager.ConnectionStrings["constrng"].ConnectionString;
            return constr;
        }

        protected void txtUserName_TextChanged(object sender, EventArgs e)
        {
            string str = getConstring();
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand("Select username,email,company,mobile from crud where username='"+txtUserName.Text+"'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
             
                txtEmail.Text = dt.Rows[0]["email"].ToString();
                txtMobile.Text = dt.Rows[0]["mobile"].ToString();
                txtCompany.Text = dt.Rows[0]["company"].ToString();
                txtEmail.Enabled = true;
                txtMobile.Enabled = true;
                txtCompany.Enabled = true;
                txtUserName.Enabled = false;
            }
        }
    }
}

Please go through the codes hope that you understood everything,write to us if you have any doubts.
About Delete Operations Part 3
/* androidsharp */

0 comments: