Sunday 26 May 2013

  • search data from database using asp.net C#


    Search/Fetch data:-Asp.Net fetching data

    Here we will create simple employee information form in the Default.aspx form.

    Note:-Dont Copy the Red Coloured Text,its only for better explanation of code.


    Default.aspx:-

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

    <!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>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div style="text-align: center">
    <div style="text-align: center">


    <table>
    <tr>
    <td style="width: 100px">
    Product ID
    </td>
    <td style="width: 100px">
    <asp:TextBox ID="txtpid" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="width: 100px">
    Product Name:-
    </td>
    <td style="width: 100px">
    <asp:TextBox ID="txtpnm" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="width: 100px">
    Product Price:-</td>
    <td style="width: 100px">
    <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" style="text-align: center">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" />
    </td>              
    </tr>
    <tr>
    <td colspan="2" style="text-align: center">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </td>
    </tr>
    </table>


    </div>
    </div>
    </form>
    </body>
    </html>


    Now we write the code to fetch/search data from the database.


    Default.aspx.cs:-

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;   //don't forget to add

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

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Connection String"); 

    //-------------------------------------------------------------------------------------------------------------

    /*connection string will be provided when you create database.first go to solution explorer->right click on site->click on add new item->select Sql server database.

    Then you can see your database in server explorer window.click on the database,so you can see connection string in the property window.Just copy it and paste in above code instead of "Connection String".

    Then add @ before starting inverted coma "".So you will not see the red line below the connection string.*/

    //----------------------------------------------------------------------------------------------------------------

    string sqlstr;
    sqlstr="select * from product where pid="+txtpid.Text+"";

    //--------------------------------------------------------------------------------------------------------
    /*in above line product is the table name and pid is the field name in the employee table.
    //if we use numeric value to pass with the field name,in above ex. txtpid.Text contains p_id which is numeric so we write txtpid.Text in between "++" and if we use string value to pass with field name (ex. employee name),then we write control name(ex. txtpid.Text) which contains string value in between '"++"'.So please be careful while passing values.*/
    //---------------------------------------------------------------------------------------------------------

      SqlDataAdapter sda=new SqlDataAdapter(sqlstr,con);

            DataSet dst = new DataSet();
            sda.Fill(dst);
    //Now we will check the condition for checking availability of data as per query.
            if(dst.Tables[0].Rows.Count != 0)
            {
                txtp_nm.Text=dst.Tables[0].Rows[0][1];
                txtp_type.Text=dst.Tables[0].Rows[0][2];
                txtp_price.Text=Convert.Tostring(dst.Tables[0].Rows[0][3];
            }

    //----------------------------------------------------------------------------------------------------------
    /*In above line we convert the dataset value in to the string because textbox control accepts only string values and dst.Tables[0].Rows[0][3] contains price of product which is numeric so we have to convert it to the string.*/

    /*In above line dst.Tables[0].Rows[0][1],it means Tables[0] is the table which is passed in the 0th position in query which is product in our example.
    Rows[0][1] it means it will return value from the 1st column/database field in the 0th row.So,in our example :
    0th column/database field contains product id,
    1st column/database field contains product name,
    2nd column/database field contains  product price 


    You can set index as per your sequence while creating the database.So Rows[0][1] returns the 0th row/tuple value from the 1st column/database field which is product name in this example,it will returns employee name which may be any xyz.*/
    //----------------------------------------------------------------------------------------------------------
            
            else
            {
                Label1.Text="Record Not Found";
            }
         }
       }

    Thats it.Now run the code through F5, and see the result.

  • 0 comments:

    Post a Comment

    Copyright @ 2013 Programming Languages.