Table Structure from SQL Server (Express Edition) 

sql_data

Web.config

<connectionStrings>
<add name="Myconnection" connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Search Data  ADO.Net Connected Model

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

using System.Configuration;
public partial class search_connected : System.Web.UI.Page
{
    SqlConnection cnn;
    SqlCommand cmd;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        cnn = new SqlConnection();
        cnn.ConnectionString = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String code = TextBox1.Text;
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from students  where rollno=@rollno";
        cmd.Parameters.AddWithValue("@rollno", int.Parse(code));
        cmd.Connection = cnn;
        cnn.Open();
        SqlDataReader sr;
        sr = cmd.ExecuteReader();
        if (sr.HasRows)
        {
            sr.Read();
            TextBox2.Text = sr.GetDecimal(0).ToString(); ;
            TextBox3.Text = sr.GetString(1);
            TextBox4.Text = sr.GetString(2);
           
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Warning!", "alert('Record Not Found')", true);
        }
        cnn.Close();
    }
}

Search Data  ADO.Net Disconnected Model

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


using System.Configuration;
public partial class search_record : System.Web.UI.Page
{
    SqlConnection cnn;
    SqlCommand cmd;
    SqlDataAdapter ada;
    DataSet ds;
    SqlParameter sp;
    protected void Page_Load(object sender, EventArgs e)
    {
        cnn = new SqlConnection();
        cnn.ConnectionString = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
            
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String code = TextBox1.Text;
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from students where rollno=@rollno";
        cmd.Connection = cnn;
        sp = new SqlParameter();
        sp.ParameterName = "rollno";
        sp.Value = code;
        cmd.Parameters.Add(sp);
      
        ada = new SqlDataAdapter();
        ada.SelectCommand = cmd;
        ds = new DataSet();
        ada.Fill(ds);

        TextBox2.Text = ds.Tables[0].Rows[0][0].ToString();
        TextBox3.Text = ds.Tables[0].Rows[0][1].ToString();
        TextBox4.Text = ds.Tables[0].Rows[0][2].ToString();
         
    }
    
}

output..

sql_1

 


How do you search by SqlDataSource..


<form id="form1" runat="server">
<div>
<div>
entere code <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Find Now!" onclick="Button1_Click" />
<br />
</div>
<hr />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Myconnection %>"
SelectCommand="SELECT * FROM [students] where (rollno)=@rollno" >
<SelectParameters>
<asp:Parameter Name="rollno" DbType="Int16" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>


Here  is Code  behind page...


using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
public partial class searchbyDataSource : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != null)
        {

            SqlDataSource1.SelectParameters["rollno"].DefaultValue = TextBox1.Text;
            DataView dv = new DataView();
            dv= (DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            foreach (DataRowView v in dv)
            {
                TextBox2.Text = v[0].ToString();
                TextBox3.Text = v[1].ToString();
                TextBox4.Text = v[2].ToString();
            }
        }
    }
}