6
Answers

Searching Records From a DataBase and Displaying In a Gridvi

chinedu Nwankwo

chinedu Nwankwo

10y
1.6k
1

 Re :

Searching Records From a DataBase and Displaying In a Gridview Using ASP.Net C#    By Vithal Wadje

 

Hi all !, Please help ! ive used the code but ran into errors

 

This is my code :

 

/////////////

 

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.Configuration;

 

namespace New_Background_application

{

public partial class WebForm10 : System.Web.UI.Page{

         public SqlConnection con;

    public string constr;

 

    public void connection()

    {

 

 

        constr = ConfigurationManager.ConnectionStrings["emp"].ToString();  

    con = new SqlConnection(constr);

        con.Open();

   

    }

        protected void Page_Load(object sender, EventArgs e)

        {

              Label1.Visible = false;

 

        }

         private void rep_bind()

                    {

                        connection();

                        string query = "select * from emp where Name like'" + TextBox1.Text + "%'";

 

                        SqlDataAdapter da = new SqlDataAdapter(query, con);

                        System.Data.DataSet ds = new System.Data.DataSet();

                        da.Fill(ds);

                        GridView1.DataSource = ds;

                        GridView1.DataBind();

                    }

 

 

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

             connection();

        string query = "select Name from emp where Name like'" + TextBox1.Text + "%'";

        SqlCommand com = new SqlCommand(query, con);

 

        SqlDataReader dr;

        dr = com.ExecuteReader();

 

 

        if (dr.HasRows)

        {

            dr.Read();

 

            rep_bind();

            GridView1.Visible = true;

 

            TextBox1.Text = "";

            Label1.Text = "";

        }

        else

        {

            GridView1.Visible = false;       

            Label1.Visible = true;

            Label1.Text = "The search Term " + TextBox1.Text + "  Is Not Available in the Records"; ;

       

        }

 

 

 

    }

        }

    }

 

////////

 

    error --- A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

 

 

Warm regards !

 
Stay Blessed

 

Chinedu Nwankwo [email protected]

 
 
 

Answers (6)