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 !
Chinedu Nwankwo [email protected]

Anupam SinghPosted Jul 28, 2014, 3:03 AM
Naga RajuPosted Jul 28, 2014, 2:37 AM
constr = ConfigurationManager.ConnectionStrings["emp"].ToString();
error msg is "Object reference not set to an instance of an object."
Could anybody solve it...
chinedu NwankwoPosted Mar 13, 2014, 12:45 PM
chinedu NwankwoPosted Mar 13, 2014, 12:45 PM
Sanjay KumarPosted Mar 12, 2014, 3:25 PM
try this
Web.config
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=SANJAY;Initial Catalog=Testdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
connectionStrings>
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
#region SqlConnection Connection
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
#endregion
private void Bind()
{
string sqlSelect = "SELECT Name FROM EmpLogin WHERE Name like'" + TextBox1.Text + "%'";
try
{
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (SqlException ex)
{
ShowMessage(ex.ToString());
}
finally
{
conn.Close();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Select query
string sqlSelect = "SELECT Name FROM EmpLogin WHERE Name like'" + TextBox1.Text + "%'";
SqlCommand cmd = new SqlCommand(sqlSelect, conn);
try
{
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
reader.Read();
Bind();
GridView1.Visible = true;
}
else
{
GridView1.Visible = false;
Label1.Visible = true;
Label1.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Records";
}
}
catch (SqlException ex)
{
ShowMessage(ex.ToString());
}
finally
{
conn.Close();
}
}
}
saurabh mittalPosted Mar 12, 2014, 10:14 AM
I think ur connection string is not right.
it is in this format:
ConfigurationManager.ConnectionStrings["connection string name"].ConnectionString;
and connection string like this:-