Fill textbox on click on the select button.


How can fill textbox on click on select button

How can fill textbox on click on select button

Today we are going to create a project for Insert, Update, Delete operation in DotNet Technology.

 Here I want to tell you which techniques I have used in my project like as-

1). SQL server 2008.

 For Database, Table, and Stored Procedure.

2). Visual Studio 2012(integrated development environment (IDE))

I. C#.

II. ASP.Net(for controls like Button, HiddenField, Gridview etc.)

III. ADO.net(for ConnectionString, DataAdapter, SqlCommand etc.)

First Create A database “APCRUD” and table “Contact

How can fill textbox on click on select button

And Second step is – Create Stored Procedure for different action as Insert data, delete data, Edit data or select data and update data etc.

//----For DataBase----//

CREATE database APCRUD

    GO
    USE APCRUD
//-----For Table-----//

CREATE TABLE Contact

     (
        ContactID int primary Key Identity(1,1),
        Name varchar(50),
        Mobile varchar(50),
        Address varchar(250)
      )

Create Stored Procedure – for Insert and Update records...

How can fill textbox on click on select button

CREATE Proc ContactCreateOrUpdate
@ContactID int,
@Name varchar(50),
@Mobile varchar(50),
@Address varchar(50)
AS
   BEGIN
      IF(@ContactID=0)
     Begin
         insert into Contact(Name,Mobile,Address) values(@Name, @Mobile, @Address)
     END
     Else
         Begin
         update Contact Set Name=@Name, Mobile=@Mobile, Address=@Address
      where  ContactID=@ContactID
     END
    END

Create Stored Procedure - for Delete records by ID

CREATE Proc ContactDeleteByID

@ContactID int
AS
Begin
  Select * from Contact where ContactID=@ContactID
END

Create Stored Procedure - for Display all records

CREATE Proc ContactViewAll

AS
Begin
  Select * from Contact
END

Create Stored Procedure - for Display record by ID

CREATE Proc ContactViewByID

@ContactID int
AS
Begin
  Select * from Contact where ContactID=@ContactID
END

Here I define .aspx page for design pattern.

--- Using HiddenField to get ContactID for any operation.

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField ID="hfContactID" runat="server" />
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>
                    <asp:Label ID="lblMobile" runat="server" Text="Mobile"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>
                    <asp:Label ID="lblAddress" runat="server" Text="Address"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>

                </td>
                <td>
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
                    <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
                </td>


            </tr>

            <tr>
                <td colspan="2" align="center">
                     <asp:Label ID="lblErrorMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
                </td>
                </tr>
            <tr>
                <td colspan="2" align="center">
                     <asp:Label ID="lblSuccessMessage" runat="server" Text="" ForeColor="Green"></asp:Label>
                </td>

            </tr>

        </table>
        <br />
        <asp:GridView ID="gvContact" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Mobile" HeaderText="Mobile" />
                <asp:BoundField DataField="Address" HeaderText="Address" />

                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkView" runat="server" CommandArgument='<%#Eval("ContactID") %>' OnClick="lnk_OnClick">View</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

How can fill textbox on click on select button

And

Here Define the .cs code for all actions  

using System;

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

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=s-pc;Initial Catalog=ASPCRUD;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            btnDelete.Enabled = false;
            FillGridView();
        }

    }

    public void Clear()
    {
        hfContactID.Value= "";
        txtName.Text = "";
        txtMobile.Text = "";
        txtAddress.Text = "";
        lblErrorMessage.Text = "";
        lblSuccessMessage.Text = "";
        btnDelete.Enabled = false;
    }

    public void FillGridView()
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        SqlDataAdapter da = new SqlDataAdapter("ContactViewAll", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataTable dtbl = new DataTable();
        da.Fill(dtbl);
        gvContact.DataSource = dtbl;
        gvContact.DataBind();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        Clear();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        SqlCommand cmd = new SqlCommand("ContactCreateOrUpdate",con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ContactID", (hfContactID.Value == "" ? 0 : Convert.ToInt32(hfContactID.Value)));
        cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
        cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text.Trim());
        cmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
        cmd.ExecuteNonQuery();
        con.Close();

        string ContactID = hfContactID.Value;

        Clear();

        if (ContactID == "")
            lblSuccessMessage.Text = "Record Save Successfully";
        else
            lblErrorMessage.Text = "Update Successfull";
        FillGridView();
    }

    protected void lnk_OnClick(object sender, EventArgs e)
    {
        int contactID = Convert.ToInt32((sender as LinkButton).CommandArgument);
        if (con.State == ConnectionState.Closed)
            con.Open();
        SqlDataAdapter SqlDa = new SqlDataAdapter("ContactViewByID",con);
        SqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
        SqlDa.SelectCommand.Parameters.AddWithValue("ContactID", contactID);
        DataTable dtbl = new DataTable();
        SqlDa.Fill(dtbl);
        con.Close();
        hfContactID.Value = contactID.ToString();
        txtName.Text = dtbl.Rows[0]["Name"].ToString();
        txtMobile.Text = dtbl.Rows[0]["Mobile"].ToString();
        txtAddress.Text = dtbl.Rows[0]["Address"].ToString();
        btnSave.Text = "Update";
        btnDelete.Enabled = true;
        FillGridView();
        lblErrorMessage.Text = "";
        lblSuccessMessage.Text = "";
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        SqlCommand SqlCmd = new SqlCommand("ContactDeleteByID",con);
        SqlCmd.CommandType = CommandType.StoredProcedure;
        SqlCmd.Parameters.AddWithValue("@ContactID", Convert.ToInt32(hfContactID.Value));
        SqlCmd.ExecuteNonQuery();
        con.Close();
        Clear();
        FillGridView();
        lblSuccessMessage.Text = "Record Delete Successfully";
    }
}

How can fill textbox on click on select button

Thank You...


  Modified On Sep-07-2019 12:18:29 AM

Leave Comment