Hi

I am very new to C#. Below is the program which i am trying.
{
string connstring = "Data Source = .\\SQLEXPRESS;Initial Catalog = SAMP; Integrated Security = True";

SqlConnection conn = new SqlConnection(connstring);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
SqlDataAdapter myadapter = new SqlDataAdapter(comm);
DataSet myset = new DataSet();
conn.Open();
comm.CommandText = "Select * From EMP ";
comm.ExecuteScalar();
myadapter.Fill(myset, "EMP");
dataGridView1.DataSource = myset;
dataGridView1.DataMember = "EMP";
conn.Close();
}
But I am getting following error. I am using sql server 2005 express edition.

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Pls anybody provide me some solution.

try using (local) instead of . as your server name

You can also connect to the database using the connection wizard

Data>Add new data source

First thing , you are Opening a Connection in a Wrong Place and you dont trap your Errors , Second thing your Connection String is incorect. You are Executing the Command object and at the same time you are using the adapter, why ?? . Your Code should look like this

string connstring = "User id =sa;Password=hello;Database=SAMP; Server =MyServer" 

SqlConnection conn = new SqlConnection(connstring);

SqlCommand comm = new SqlCommand();

comm.CommandText = "Select * From EMP ";

comm.Connection = conn;

DataSet myset = new DataSet();

SqlDataAdapter myadapter = new SqlDataAdapter();

myAdapter.SelectedCommand = comm;

try
{

conn.Open();

myadapter.Fill(myset, "EMP");
}
catch(SQLException)
}
Finally
{
conn.Close();
}

Your Code Is Screeming "Hey put me inside a Function or in a Class" lol , So try to put this code in a class or a Function and in your Form you can Bind your Gridview like this

dataGridView1.DataSource = myset;
dataGridView1.DataMember = "EMP";

Hope this helps

hey gys....can u help me out....its quite urgent..
actually i downloaded a project....it has datasource written as "local"..
now when i run it says..login failed for (my pc name)...
what shud i do....? plz help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.