Error while converting string to SqlConnection
Asked By: Lost_in_CS
Originally Asked On: 2014-01-05 19:38:58
Asked Via: stackoverflow
I’m getting this error:
Cannot implicitly convert type string to System.Data.SqlClient.SqlConnection
Here is my C# code.
protected void Button1_Click(object sender, EventArgs e) { //login button SqlConnection sqlConn = "Your Conncetion String"; //The error is here sqlConn.Open(); SqlCommand sqlComm = new SqlCommand(); sqlComm.CommandText = String.Format("select * from users where userName=@userName and password=@password"); sqlComm.Parameters.AddWithValue("@userName", TextBox1.Text.Trim()); sqlComm.Parameters.AddWithValue("@password", TextBox2.Text.Trim()); sqlComm.CommandType = CommandType.Text; sqlComm.Connection = sqlConn; SqlDataReader sqlRead = sqlComm.ExecuteReader(); if (sqlRead.Read()) { Session["username"] = sqlRead["username"]; } // SqlRead.Close(); //sqlConn1.Close(); Response.Redirect("Default.aspx"); }
Can someone explain what this error means and how to fix it?
He received 3 answers
eventually accepting:
‘s answer to
Error while converting string to SqlConnection
The answer with the highest score with 5 points was:
Szymon’s answer to
Error while converting string to SqlConnection
I understand you have a connection string and want a
SqlConnection
object. You can use one of its constructors:string connString = "Your Conncetion String"; // valid connection string var sqlConn = new SqlConnection(connString);
It will be best if you use
using
keyword as this will take care of closing the connection properly. You should also useusing
for yourSqlReader
.using (var sqlConn = new SqlConnection(connString)) { sqlConn.Open(); // the rest of the code }
If the selected answer did not help you out, the other answers might!
All Answers For: Error while converting string to SqlConnection
Szymon’s answer to
Error while converting string to SqlConnection
I understand you have a connection string and want a
SqlConnection
object. You can use one of its constructors:string connString = "Your Conncetion String"; // valid connection string var sqlConn = new SqlConnection(connString);
It will be best if you use
using
keyword as this will take care of closing the connection properly. You should also useusing
for yourSqlReader
.using (var sqlConn = new SqlConnection(connString)) { sqlConn.Open(); // the rest of the code }
Ashley Medway’s answer to
Error while converting string to SqlConnection
Change
SqlConnection sqlConn = "Your Conncetion String";
To
SqlConnection sqlConn = new SqlConnection("Your Conncetion String");
meda’s answer to
Error while converting string to SqlConnection
it should be
SqlConnection sqlConn = new SqlConnection("Your Conncetion String");
You can read it from web.config
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
now this error The name ‘ConfigurationManager’ does not exist in the current context
You have to add a reference to the
System.Configuration.dll
then add
using System.Configuration;
to your class.You can now access your config file like this:
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
Of course, you should really check out the original question.
The post Error while converting string to SqlConnection [ANSWERED] appeared first on Tech ABC to XYZ.