Example : How to Search or Find unique record from Sql Server database in Asp .net using C#.

Write code in 'Trial.aspx.cs' (C# Sharp File)


using System.Data.SqlClient;
using System.Data;

---------------------------------------------------------------------------------------
public partial class Trial : System.Web.UI.Page
  {  
 
      //One method of Connectivity code(use any one)

      SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB; AttachedDbFilename Initial Catelog=SITEDB; Integrated Security=True;");
        
        public static int i;
        SqlConnection con;
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        DataRow dr;

---------------------------------------------------------------------------------------
protected void BtnSearch_Click(object sender, EventArgs e)
        {
            string cs = "data source =.; database= SITEDB; integrated security=SSPI";  //Another Connectivity code
            SqlConnection con = new SqlConnection(cs);

            con.Open();
            
            //string sdata = SearchData.Text;
            da = new SqlDataAdapter("select * from Traildb where Slno='"+SearchData.Text+"'", con);
            da.Fill(dt);

            i = 0;
            dr = dt.Rows[i];

            TxtSlno.Text = Convert.ToString(dr[0]);
            TxtName.Text = Convert.ToString(dr[1]);
            TxtPass.Text  = Convert.ToString(dr[2]).Trim();

            TxtPass.Attributes["value"] = TxtPass.Text;
            Response.Write(TxtPass.Text);

            TxtEmail.Text = Convert.ToString(dr[3]);
            DtDob.Text = Convert.ToString(dr[4]);

            String x = Convert.ToString(dr[5]).Trim();
            if (x == "Male" )
            {
                RdbGender1.Checked=true;
            }
            else if(x== "Female")
            {
                RdbGender2.Checked = true;
            }
            else
            {
                RdbGender1.Checked = false;
                RdbGender2.Checked = false;
            }

            string Mat = Convert.ToString(dr[6]).Trim();
            if (Mat == "Matric")
            {
                ChkMatric.Checked = true;
            }

            string Inte = Convert.ToString(dr[7]).Trim(); ;
            if (Inte == "Inter")
            {
                ChkInter.Checked = true;
            }

            string Grd = Convert.ToString(dr[8]).Trim();
            if (Grd == "Graduation")
            {
                ChkGrad.Checked = true;
            }

            string Pgrd = Convert.ToString(dr[9]).Trim();
            if (Pgrd == "Post Graduation")
            {
                ChkPgrad.Checked = true;
            }

            TxrAdd.Text = Convert.ToString(dr[10]);
            CmbCity.Text = Convert.ToString(dr[11]).Trim();
            NmbPin.Text = Convert.ToString(dr[12]).Trim();
            
            
            con.Close();
        }      
       
    }





Write code in 'Trial.aspx' (Html design page file)



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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />        
    
</head>
<body>
       <section>
            <div class="container my-4">
                <h1>This is Test Page</h1>
            </div>
        </section>
        
        <section>
            <div class="container my-4">
                <div class="card">
                    <div class="card-header">
                        Registration Page
                    </div>
                    
                    <div class="card-body">
                        <form runat="server" id="form1" method="post" enctype="multipart/form-data">
                           <div class="form-row">
                               <div class="form-group col-md-6">
                                   <asp:DropDownList ID="SearchData" CssClass="form-control" runat="server">                                    
                                    <asp:ListItem Value="" Text="-Select-" />
                                    <asp:ListItem Value="1" Text="1" />
                                    <asp:ListItem Value="2" Text="2" />
                                    

                                </asp:DropDownList>
                               </div>
                                 
                               <div class="form-group  col-md-6">
                                   <asp:Button CssClass="btn btn-info form-control" ID="BtnSearch" Text="Search" runat="server" OnClick="BtnSearch_Click"/>
                               
                               </div>     
                               
                            </div>
                            <div class="form-group">
                                <asp:Label for="TxtSlno" Text="" runat="server" /> Slno
                               <asp:TextBox ID="TxtSlno"  CssClass=" form-control" onkeypress="return (event.keyCode>=48 && event.keyCode<=57 );" runat="server" OnTextChanged="TxtSlno_TextChanged" />
                                
                                <%-- <asp:TextBox ID="TxtSlno" CssClass=" form-control" onkeydown="return (!(event.keyCode>=65 && event.keyCode<=90 ) && event.keyCode!=32);" runat="server" /> --%>

                            </div>

                             <div class="form-group">
                                <asp:Label for="TxtName" Text="" runat="server" /> Name
                                <asp:TextBox ID="TxtName" CssClass="form-control " onkeypress="return ((event.keyCode>=97 && event.keyCode<=122 || event.keyCode>=65 && event.keyCode<=90) );" runat="server" />
                            </div>

                             <div class="form-group">
                                <asp:Label for="TxtPass" Text="" runat="server" /> Password
                               <asp:TextBox ID="TxtPass" TextMode="Password" CssClass="form-control" runat="server"></asp:TextBox>
                            </div>

                            <div class="form-group">
                                <asp:Label for="TxtEmail" Text="" runat="server" /> Email-Id
                               <asp:TextBox ID="TxtEmail" TextMode="Email" CssClass="form-control" runat="server"></asp:TextBox>
                            </div>
                            <div class="form-group">
                                <asp:Label for="DtDob"  runat="server" />Date of Birth
                                <asp:TextBox ID="DtDob" runat="server" TextMode="Date" CssClass="form-control"/>
                            </div>
                            <div class="form-group">
                                <asp:Label for="RdbGender" runat="server" /> Gender
                                <div class=" form-check">
                                    <asp:RadioButton ID="RdbGender1" GroupName="rd1"  Value="Male" runat="server" /> Male
                                    <asp:RadioButton ID="RdbGender2" GroupName="rd1" Value="Female" runat="server" /> Female
                                </div>
                            </div>


                            <div class="form-check-label">
                                <asp:Label runat="server"/>Qualification
                            </div>

                            <div class="form-check form-check-inline">                                
                                <asp:CheckBox ID="ChkMatric" CssClass=" form-check-input" runat="server" />
                                <asp:Label for="ChkMatric" CssClass="form-check-label" runat="server" /> Matric
                            </div>

                            <div class="form-check form-check-inline">                                
                                <asp:CheckBox ID="ChkInter" CssClass=" form-check-input" runat="server" />
                                <asp:Label for="ChkInter" CssClass="form-check-label" runat="server" /> Intermediate
                            </div>

                            <div class="form-check form-check-inline">                                
                                <asp:CheckBox ID="ChkGrad" CssClass=" form-check-input" runat="server" />
                                <asp:Label for="ChkGrad" CssClass="form-check-label" runat="server" />Graduation
                            </div>
                            <div class="form-check form-check-inline">                                
                                <%--<asp:CheckBox ID="CheckBox1" Text="&nbsp;Post Graduation" CssClass="form-check-input" runat="server" />--%>
                                <asp:CheckBox ID="ChkPgrad" CssClass="form-check-input" runat="server" />
                                <asp:Label for="ChkPgrad" CssClass="form-check-label" runat="server" />Post Graduation
                            </div>
                            

                            <div class="form-group">
                                <asp:Label for="TxrAdd" runat="server" />Address
                                <%--<textarea id="TextArea1" class="form-control" cols="20" rows="2"></textarea>--%>

                                <asp:TextBox runat="server" ID="TxrAdd" mlkTextMode="MultiLine" Height="100" CssClass="form-control" />
                            </div>
                            
                            <div class="form-group">
                                <asp:Label for="CmbCity" runat="server" />City
                                <asp:DropDownList ID="CmbCity" CssClass="form-control" runat="server">                                    
                                    <asp:ListItem Value="" Text="-Select-" />
                                    <asp:ListItem Value="Patna" Text="Patna" />
                                    <asp:ListItem Value="Delhi" Text="Delhi" />
                                    <asp:ListItem Value="Mumbai" Text="Mumbai" />
                                    <asp:ListItem Value="Lakhnau" Text="Lakhnau" />
                                    <asp:ListItem Value="Jharkhand" Text="Jharkhand" />
                                    <asp:ListItem Value="Panji" Text="Panji" />
                                    <asp:ListItem Value="Itanagar" Text="Itanagar" />

                                </asp:DropDownList>
                            </div>
                            <div class="form-group">
                                <asp:Label for="NmbPin" runat="server" />Pin Code
                                <asp:TextBox ID="NmbPin" CssClass="form-control" TextMode="Number" MaxLength="10" runat="server" />
                            </div>
                            <div class="form-group">
                                <asp:Label for="FileUpload" runat="server" />Upload Picture
                                <asp:FileUpload ID="FileUpload1" CssClass="custom-file" runat="server" />
                            </div>

                            <div class="form-group">
                                <asp:Button CssClass="btn btn-success" ID="Submit" Text="Submit" runat="server" OnClick="Submit_Click"  />
                                <asp:Button CssClass="btn btn-info" ID="Reset" Text="Reset"  runat="server" OnClick="Reset_Click"  />
                                <asp:Button CssClass="btn btn-danger" ID="Delete" Text="Delete"  runat="server" OnClick="Delete_Click" />
                                <asp:Button CssClass="btn btn-secondary" ID="Update" Text="Update"  runat="server" OnClick="Update_Click" />
                            </div>
                            <div>
                         <asp:GridView ID="GridView1" runat="server">       </asp:GridView>
                    </div>
                        </form>

                    </div>

                </div>
            </div>
        </section>
        


        <!-- Bootstrap JS, Popper.js, and jQuery -->
        <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
      
</body>
</html>

Loading

Categories: ASP .Net

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.