My code:
if (Page.IsValid)
{
DataSet.UsersDataTable oUserDataTable =
new DataSet.UsersDataTable();
DataSetTableAdapters.UsersTableAdapter oUserTableAdapter =
new DataSetTableAdapters.UsersTableAdapter();
oUserTableAdapter.FillUserByUserName(oUserDataTable, txtUserName.Text);
if (oUserDataTable.Count!=1)
{
string strErrorMessage =
"UserName Or Password Is Not Correct ! Please Try Again . . . ";
DisplayErrorMessage(strErrorMessage);
return;
}
DataSet.UsersRow oUserRow = oUserDataTable[0];
if (string.Compare(oUserRow.Password.Trim(),txtPassword.Text.Trim(),false)!=0)
{
string strErrorMessage =
"UserName Or Password Is Not Correct ! Please Try Again . . . ";
DisplayErrorMessage(strErrorMessage);
return;
}
if (oUserRow.IsUserActive==false)
{
string strInformationMessage =
string.Format("Dear {0} You Should Not Login At This Time , Please Contact Support",txtUserName.Text);
DisplayInformationMessage(strInformationMessage);
return;
}
I get an error:
Additional information: 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)
first of all check if db connection can open, if yes, then is good to go, otherwise throw exception and check if the connection string is correct. Following code can help you for DB connection checking
try
{
using (SqlConnection conn = new SqlConnection("connection"))
{
conn.Open();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Invalid Connection String");
}
add this code before this line DataSet.UsersDataTable oUserDataTable =
new DataSet.UsersDataTable();
Related
My connection string looks like this:
Data Source=sql88;Initial Catalog=Osm;User ID=foouser;Password=foopsw
The code that creates connection string looks like this:
var con = new SqlConnectionStringBuilder();
if (string.IsNullOrEmpty(model.Username) && string.IsNullOrEmpty(model.Password))
con.IntegratedSecurity = true;
else
{
con.UserID = model.Username;
con.Password = model.Password;
}
con.DataSource = model.Host ?? string.Empty;
con.InitialCatalog = model.Database ?? string.Empty;
return con;
My application creates database structure at SQL Server, then inserting data. So sometimes the applcation works approximately 14 hours. The code that executes SQL at SQL Server looks like this:
using (var cmd = con.CreateCommand())
{
cmd.CommandTimeout = int.MaxValue;
cmd.CommandText = sql;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
var test = ex.Message;
throw;
}
}
Sometimes application works well, but sometimes application throws an 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: TCP Provider, error: 0 - No such host is known.) (Microsoft
SQL Server, Error: 11001)
I've read this page, however, in my view this is not my case because sometimes my application works well.
when i tried to run this program in visual studio 2010 its shows an error. Like this "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)"
public partial class tcregistration : Form
{
SqlConnection conn = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Users/dce 3/documents/visual studio 2010/Projects/TC_Maker/TC_Maker/TC_REG.mdf;Integrated Security=True;User Instance=True");
public tcregistration()
{
InitializeComponent();
}
private void insert_Click(object sender, EventArgs e)
{
string gender = string.Empty;
if (rbmale.Checked)
{
gender = "M";
}
else if (rbfemale.Checked)
{
gender = "F";
}
string tcrecieved = string.Empty;
if (rbyes.Checked)
{
tcrecieved = "Y";
}
else if (rbno.Checked)
{
tcrecieved = "N";
}
try
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand ("TCAddorUpdate",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#mode","Add");
cmd.Parameters.AddWithValue("#tcnumber",txttcno.Text.Trim());
cmd.Parameters.AddWithValue("#name",txtname.Text.Trim());
cmd.Parameters.AddWithValue("#dob",dtpdob);
cmd.Parameters.AddWithValue("#religion",txtrelig.Text.Trim());
cmd.Parameters.AddWithValue("#caste",txtcaste.Text.Trim());
cmd.Parameters.AddWithValue("#sex",gender);
cmd.Parameters.AddWithValue("#doa",dtpdoa);
cmd.Parameters.AddWithValue("#regno",txtregno.Text.Trim());
cmd.Parameters.AddWithValue("#dor",dtpdor);
cmd.Parameters.AddWithValue("#dept",txtdept.Text.Trim());
cmd.Parameters.AddWithValue("#sem", combosem);
cmd.Parameters.AddWithValue("#ifqulify",txtqualified.Text.Trim());
cmd.Parameters.AddWithValue("#conduct",txtconduct.Text.Trim());
cmd.Parameters.AddWithValue("#applieddate",dtpdoapp);
cmd.Parameters.AddWithValue("#ifrecieved",tcrecieved);
cmd.Parameters.AddWithValue("#receiveddate",dtpdor);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Inserted Successfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Error Message");
}
finally
{
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)
This is not a programming Problem, but a networking/connection string one.
Connection Strings are their own area of experetise, way outside the normal programmers knowledge. Luckily there is a page for it: https://www.connectionstrings.com/sql-server/
It turns out even when attaching, you have to supply a database name: "Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection."
And as others mentioned, you got the wrong kind of slashes too.
Pretty off topic, but exception handling is a pet peeve of mine. And yours has some of the serioues mistakes. Like catching exception and only exposing the message. Those are 2 Cardinal sins. Thee are two article on the thematic I link often:
https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/
https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET
I have a console app and want to connect other windows server where I have already created user, database table. In conf.xml I have
<configuration>
<crypto location="172.16.10.34" database="Crypto_Pan" username="PinbySms" password="ASDasd123"/>
</configuration>
then my code is :
Dictionary<string, string> d = conf.getCrypto();
SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();
bu.DataSource = d["location"];
bu.NetworkLibrary = "DBMSSOCN";
bu.InitialCatalog = d["database"];
bu.IntegratedSecurity = false;
bu.UserID = d["username"];
bu.Password = d["password"];
SqlConnection thisConnection = null;
try
{
thisConnection = new SqlConnection(bu.ConnectionString);
thisConnection.Open();
Console.WriteLine("success connected");
}
catch (Exception e)
{
Console.WriteLine("exeption: " + e.ToString());
thisConnection.Close();
}
I have an error when my app try to connect this database:
[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.
Does anybody have any suggestions?
there was not default port in sql configuration PROTOCOL MSQSQLSERVER TCP/IP , i changed it to 1433 and now it's ok.
While trying to connect the oracle database I am getting the following 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)"}
Problem may be silly one but this is my first time with database so need help:
My code is:
static void Main(string[] args)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source=ORCL_BOA; database=BOANEWDOC;User Id=BOANEWDOC;Password=BOANEWDOC;Trusted_Connection=true";
conn.Open();
//code
}
you may need to reference Oracle.ManagedDataAccess.dll on ODTwithODAC121012.zip you can download from oracle site.
do not use System.Data.OracleClient as it is obsolete.
var connection = new OracleConnection(YourConnectionString);
try
{
connection.Open();
//AMK: Do some stuff with the db
}
catch (Exception exception)
{
//AMK: do some other stuff in case of error
}
finally
{
if(connection !=null && connection.State==ConnectionState.Open)
connection.Close();
}
I have a form that inserts data into a mysql database located at Godaddy. This sql code will work when I test locally. But when I push it out to Godaddy, I get the error: "Cannot perform CAS Asserts in Security Transparent methods". I have googled and looked on SO for this error. There wasn't much out there, but I did find a couple things talking about setting the CAS trust level on the server. I did find that in the asp.net settings in my Godaddy account and set that to "full". But it did not fix the error.
Here is the c#/mysql code that is throwing the error:
try
{
using (MySqlConnection connection = new MySqlConnection(appConfig._strMYSqlContact))
{
// create the sql command
MySqlCommand myCommand = new MySqlCommand(mySQL, connection);
// add parameters
myCommand.Parameters.AddWithValue("?groupname", model.grName);
myCommand.Parameters.AddWithValue("?groupsize", model.grSize);
myCommand.Parameters.AddWithValue("?contactname", model.coName);
myCommand.Parameters.AddWithValue("?address", model.address);
myCommand.Parameters.AddWithValue("?city", model.city);
myCommand.Parameters.AddWithValue("?state", model.state);
myCommand.Parameters.AddWithValue("?zipcode", model.zip);
myCommand.Parameters.AddWithValue("?county", model.county);
myCommand.Parameters.AddWithValue("?email", model.email);
myCommand.Parameters.AddWithValue("?phone", model.phone);
myCommand.Parameters.AddWithValue("?calltime", model.bestTime);
myCommand.Parameters.AddWithValue("?message", model.message);
myCommand.Parameters.AddWithValue("?howheard", model.howHeard);
myCommand.Parameters.AddWithValue("?moreaboutclubs", model.tellUsMore);
myCommand.Parameters.AddWithValue("?dateentered",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
connection.Open();
isSuccess = myCommand.ExecuteNonQuery();
}
if (isSuccess == 1)
{
TempData["message"] = "Thank you for contacting us. Someone will be in contact with you within 48 hours.";
return RedirectToAction("Success");
}
else
{
ModelState.AddModelError("", "Error inserting into database.");
}
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
}