C# local database error - c#

i am having an error while connecting the local service database of C# in VS 2017. The app is just for testing and the error i am having the con.open() line. I have placed a data gridview in the app with dataset and it is showing all the values inside the db in gridview but when i use my button to execute the INSERT Query it shows up an error. This is the error below
System.Data.SqlClient.SqlException: '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: 50
- Local Database Runtime error occurred. Specified LocalDB instance name is invalid. )'
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C: \\Users\\Shahrukh Khan\\source\\repos\\checkingsetup\\testingdb\\Database123.mdf;Integrated Security=True");
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into table1 values('"+textBox1.Text+"')";
cmd.ExecuteNonQuery();
cmd.CommandText = "select * from table1";
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}

Related

C# sqlDataAdapter issue

all I try to load data from sql by following code using SQL server. I checked connection is normal and most sql are available to load. But in certain cases, I can't load data with following error:
"The conversion of the varchar value '2863289685' overflowed an int column.
The statement has been terminated."
and that sql is runnable in sql server management studio, but can't load by following code, please help me with possible solution, thanks!
try
{
SqlCommand command = new SqlCommand(sql, conn);
command.CommandTimeout = 0;
SqlDataAdapter sqlDa = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
Console.WriteLine(ds.Tables.Count);
dt = ds.Tables[0];
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

I can't connect to SQL Server in C# [duplicate]

This question already has answers here:
How to connect to LocalDb
(12 answers)
Closed 1 year ago.
class ContactsRepository : IContactsRepository
{
private string connectionString = "Data Source=(LocalDB);Initial Catalog=Contact_DB;User ID=****;Password=**********";
public bool Delete(int ContactID)
{
throw new NotImplementedException();
}
public DataTable SelectAll()
{
string query = "Select * From MyContacts";
SqlConnection connection = new SqlConnection(connectionString);
Sql command = new SqlCommand(query, connection);
DataTable data = new DataTable();
adapter.Fill(data);
return data;
}
}
I get this error:
System.Data.SqlClient.SqlException: 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: 51 - An instance name was not specified while connecting to a Local Database Runtime. Specify an instance name in the format (localdb)\instance_name.
https://www.connectionstrings.com/sql-server-2017/
Check here for correct connection string
and also you need "connection.Open()" before run command
public class PullDataTest
{
// your data table
private DataTable dataTable = new DataTable();
public PullDataTest()
{
}
// your method to pull data from database to datatable
public void PullData()
{
string connString = #"your connection string here";
string query = "select * from table";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
// this will query your database and return the result to your datatable
da.Fill(dataTable);
conn.Close();
da.Dispose();
}
}

Creating social website in asp.net

I'm creating a social Networking Website for that I used "Social Networking Website in ASP.NET - Open Source" from
http://www.c-sharpcorner.com/UploadFile/rahul4_saxena/social-networking-website-in-Asp-Net-open-source-project/
When I tried to run the project on my Visual Studio 2012 edition it is showing error like below,
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)
Source Error:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for DataBaseClass
/// </summary>
public class DataBaseClass
{
SqlDataAdapter da;
SqlConnection con;
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public DataBaseClass()
{
//
// TODO: Add constructor logic here
//
}
public void ConnectDataBaseToInsert(string Query)
{
con = new SqlConnection(#"Data Source=Veena-SQLServer2008; Initial Catalog=RNetworkingWebApplication;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public DataSet ConnectDataBaseReturnDS(string Query)
{
ds = new DataSet();
con = new SqlConnection(#"Data Source=Veena-SQLServer2008; Initial Catalog=RNetworkingWebApplication;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
public DataTable ConnectDataBaseReturnDT(string Query)
{
dt = new DataTable();
con = new SqlConnection(#"Data Source=Veena-SQLServer2008; Initial Catalog=RNetworkingWebApplication;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return dt;
}
}
Visual studio pointing the error here da.Fill(dt).
I have spent 2 days to debug this error but still I couldn't. Please help me.
It is very simple. Your code is not able to connect to the database. Open Server Explorer in Visual Studio and connect to your database to confirm you are able to connect. And then get your connection string right.
And you do have a database, right?

connecting to database in c# via IP\DEV

Im Trying to connect to a database on a SQL server Located on 192.168.100.42\DEV but i keep getting a network related error see below by code and then the error , If any one can help it would be appreciated
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string connectionString = "Data Source=192.168.100.42\Dev;Initial Catalog=UNIS;User ID=sa;Password=Password1!";
using (SqlCommand scmd = new SqlCommand("dbo.searchName"))
{
scmd.CommandType = CommandType.StoredProcedure;
scmd.Parameters.Add(new SqlParameter("#name", cbUserName.Text));
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter dataAdapter = new SqlDataAdapter(scmd);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
conn.Open();
scmd.Connection = conn;
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView2.ReadOnly = true;
dataGridView2.DataSource = ds.Tables[0];
conn.Close();
}
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)

Cant Export to XML

I have a sql compact database connected to my website called Sort.sdf, with a table called "sort". I need to export all the records from the table to an XML file, but I keep getting the following error and cannot find a solution:
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)
I have used the same connection to successfully insert data into the database, so I don't think there is a problem there.
SqlCeConnection conn = new SqlCeConnection("Data Source=|DataDirectory|Sort.sdf");
conn.Open();
DataSet ds = new DataSet();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * from Sort", conn.ConnectionString);
da.Fill(ds); //error on this line
ds.WriteXml(#"c:\temp\output.xml", XmlWriteMode.WriteSchema);
SqlCeConnection cnn = new SqlCeConnection("Data Source=|DataDirectory|Sort.sdf");
cnn.Open();
SqlCommand cmd = new SqlCommand("select * from sort", cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
StreamWriter xmldoc=new StreamWriter(Server.MapPath("~/Testdo.xml"), false);
ds.WriteXml(xmldoc);
xmldoc.Close();

Categories