How can I handle this error? - c#

I'm a new developer with c#, I created a c# project and I need to access the SQL database to perform the SELECT statement
and I got this error in this figure
My connection statement is correct, so what's wrong with it ?!
I tried the mentioned solutions and I got this error
does anyone know how to handle it ?!

The error is telling you what to do, your connection is not open yet. Open it like:
con.Open();
Before executing your command.
Couple of things for your code, Use parameterized query, this will save you from SQL Injection, also use using statement which will ensure disposal of connection object.
using (SqlConnection con = new SqlConnection("connection string"))
using(SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID=#EmpID", con))
{
cmd.Parameters.AddWithValue("#EmpID", id.Text);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
//..... your rest of the code
}
EDIT:
For your edited part of question, you are having issue with your SQL Server not allowing remote connection. You have to enable it.
See: How to enable remote connections in SQL Server

Reader needs open connection
Put con.Open() before executing reader
SqlCommand cmd = newSqlCommand("SELECT EmpName from Employee where EmpID =" +id.Text,con);
con.open(); //Open connection
SqlReader Read = cmd.ExecuteReader();
if (Read.Read())
{
Position =Read[0].tostring();
}
read.close();
con.close();//Close connection after reader finishes reading
con.Dispose();

Related

Connecting to local database

I am trying to create to a local database via a mdf file, like so:
scon = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDBFilename=|DataDirectory|\articles.mdf; Integrated Security=True");
scon.Open();
scmd = new SqlCommand("Insert INTO articles(url) VALUES(#url)");
scmd.Parameters.AddWithValue("#url", "http://google.com");
scmd.ExecuteNonQuery();
MY mdf file is in the root folder, and in the debug folder too. When I run the following code I get an error saying the following:
I can't use the full connection path because it's a long url with spaces, here is my file structure:
My database exists:
How can I fix this so I can connect to my database?
Pass the connection object to the SqlCommand constructor
scmd = new SqlCommand("Insert INTO articles(url) VALUES(#url)", scon);
The connectionstring is fine, the error message informs you that it is not possible to execute a command if the database is not known. The SqlConnection contains this information and you need to pass it to your command.
Another possibility is through the
scmd.Connection = scon;
but, personally, I prefer to pass that info in the constructor.
Final but really important note:
SqlConnection and SqlCommand are disposable objects. You should always dispose these kind of objects. The using statement is the correct method
using(scon = new SqlConnection(....))
using(scmd = new SqlCommand("Insert INTO articles(url) VALUES(#url)",scon))
{
scon.Open();
scmd.Parameters.AddWithValue("#url", "http://google.com");
scmd.ExecuteNonQuery();
}
The Problem With Your Code Is That You Have A Command and a Connection But There Is Nothing To Till The Command To Use This Connection Object ... You Can Use The SqlCommand Constructor To Do That
scmd = new SqlCommand("Insert INTO articles(url) VALUES(#url)",scon)
Or Use The Connection Property Of The SqlCommand Class Like This
scmd.Connection = scon
Consider Adding Using To Your SQL Connection ... Or Else You Will Have To Manually Close The Connection By Calling scon.Close();
if You Didn't Do either Of Those You Will Run Into An Exception If Your Tried To Open The Connection Again While It's Already Open

Can I use Store procedure without EF in .Net, If yes then How?

1) Can I use Store procedure without EF in .Net, If yes then How??
I am new in Development,I am developmening windows application, and i am using ADO.Net dataset to access data but now my windows application is running slowly, I want to speed up it data access process...
You can use SqlCommand to execute sql commands and stored procedures.
But before reading the sample...
Please note
The performance of your application usually doesn't change using stored procedure instead of normal command or entity framework. Entity framework doesn't have performance issue itself.
You should find your performance issue some where else.
You can use any good tools in a bad way. So changing the tools is not the ultimate solution. The solution may be using the right tools in right way.
Stored Procedure Sample
For example you can see this sample:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
//If your procedure has parameters you can add parameters too
//cmd.Parameters.AddWithValue("parameter", "value");
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();

SqlConnection, Getting error and cannot connect to the database (occurred in System.Data.dll)

I have just stared to learn C# 2 Weeks ago so I dont know much but right now I just want to make my first program I don't really care about the security flaw within ATM as I will fix these with time when I know a better solution.
So I got this error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: A severe internal connection error has occurred.
And I have been sitting with this error for 2 days now modifying my code and searching the internet for solutions with no luck. So I turn here as I see there are some experts here.
Code with error:
using (SqlConnection con = new SqlConnection("server=SERVERIP,3306;Integrated Security=True;database=data;uid=USER;password=PASS"))
{
con.Open(); //ERROR HERE
using (SqlCommand cmd = new SqlCommand("insert into info(Datum,Timmar,Rast) Values(#Datum,#Timmar,#Rast)", con))
{
cmd.Parameters.AddWithValue("#Datum", textBox1.Text);
cmd.Parameters.AddWithValue("#Timmar", textBox2.Text);
cmd.Parameters.AddWithValue("#Rast", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Sparat!");
}
}
So basically what I am trying to do with this program is so that the data that the user types into the textboxes gets saved in the database.
When I type word with full capital letters I mean that it's something else there eg. Ip and password etc.
You are using the wrong connection class. You are trying to talk to a MySQL database as if it were a SQL Server database.
You need to use MySqlConnection, not SqlConnection. MySqlConnection is in MySQL Connector/NET, which can also be installed via NuGet.
Your code should look like the following instead.
using (MySqlConnection con = new MySqlConnection("server=SERVERIP;port=3306;database=data;uid=USER;password=PASS"))
{
con.Open(); // Hopefully no error here any more
using (MySqlCommand cmd = new MySqlCommand("insert into info(Datum,Timmar,Rast) Values(#Datum,#Timmar,#Rast)", con))
{
cmd.Parameters.AddWithValue("#Datum", textBox1.Text);
cmd.Parameters.AddWithValue("#Timmar", textBox2.Text);
cmd.Parameters.AddWithValue("#Rast", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Sparat!");
}
}
You have Integrated Security=true AND uid=USER;password=PASS
If you are providing username and password you shouldn't have Integrated Security=true

How to close a forgotten connection?

I created a connection and an SqlReader but forgot to close both of them:
SqlConnection conn = new SqlConnection("connection info");
conn.Open();
string sql = "SQL command HERE";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
Now when try to run the code again it always gives me this error:
System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
This link told me how to properly open and close a connection but didn't explain anything on how to close one still running.
I tried shuting down the pc, I tried looking into the database's options on SQL server (found none useful)... I changed the code to do just the close of both the connection and the reader (it compiled and runned but the problem remained after changing back the code).
How can I close this "ghost" connection? Is there any way (brute force) to close all running connections?
[EDIT:] I couldn't really solve the problem. The workaround was to add MultipleActiveResultSets=true to the connection string
I don't think you can access the ghost object, for future, just use using construct where it's possible:
using(SqlConnection conn = new SqlConnection("connection info"))
{
conn.Open();
string sql = "SQL command HERE";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
....
}
Wrap the creation in a using statement - this will always ensure the connection gets closed:
using(SqlConnection conn = new SqlConnection("connection info"))
{
// your code
}
Looking at all the answers, they seem to tell you how to avoid the problem.
If I'm not mistaken, what you mean is that a connection exists on both the client (your PC) and the server (The sql server) because you forgot to close it, and you're worried about it hanging out there forever.
Think of your connection to the server as a phone conversation. I could hang up on you, but it takes a few seconds for your phone to realize the connection is lost. You may sit there wondering if I've hung up, or just stopped talking. You really don't know. This is what happens on the server when a connection isn't closed properly. (On older landlines, you could leave the phone off the hook and tied up the line indefinitely.)
By closing the connection in code, you are effectively telling the server to close their end of the connection before closing your own. if you FAIL to close the conneciton, it will be closed on your end when the program exits or if you reboot, but the server may sit there with an open connection. (Think of someone sitting there wondering "Did he just hang up on me?")
If I'm not mistaken, what you want to get to is closing it at the SQL server end. (Getting them to "hang up".)
After rebooting, it is absolutely closed on your end. It should clear on its own at the server.
However, if you want to do it yourself, you can clear it at the server in code end using this info: How do you kill all current connections to a SQL Server 2005 database?
A far easier approach would be to just do it in SQL Server Management Studio as described here: http://www.mikebevers.be/blog/2009/07/kill-open-sql-processes-connections/
All of these answers tell you how to avoid the problem, but they don't explain what the problem is.
A SqlDataReader provides forward-only data access, which means that once you have used it and are done, you must close create a new one. See this blog for a detailed explanation. Basically, if you don't close the DataReader, then underthehood it will remain open dedicated to that connection and command.
As others have stated, its best to ensure you close all your resources.
using (SqlConnection connection = new SqlConnection("...")) {
connection.Open();
string sql = "SQL command HERE";
using (SqlCommand cmd = new SqlCommand(sql, con))
using (SqlDataReader reader = cmd.ExecuteReader()) {
// do your stuff
}
}
Truth be told even when you "close" or "dispose" of a connection it does not really go away unless you explicitly disable Pooling in your connection string. You can however do this
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.clearpool.aspx
I know this is an old post, and this may help no one. But I saw a opportunity to post what I saw wrong with this question.
First, you are creating a SqlConnection named conn but in your SqlCommand named cmd you are calling con as your connection. This is a problem:
SqlConnection conn = new SqlConnection("connection info");
conn.Open();
string sql = "SQL command HERE";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
This might be why it's giving you the error:
System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
Second, to close a conn after you are done you use:
conn.Close();
Third, to close a SqlDataReader you use:
reader.Close();
But you just assigned the SqlDataReader to reader. You never actually opened the SqlDataReader. To open it use:
reader.Read();
Or:
while (reader.Read())
{
// Code
}
Now a proper way to initilaize a connection and a SqlDataReader while opening and close them:
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM TableName;";
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
strCol1 = reader.GetValue(0).ToString();
}
reader.Close();
}
conn.Close();
}

Application Role in Program connecting the SQL Server

I have a program which would use the Application Role to write data to a SQL Server 2005.
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlCon;
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText =
"EXEC sp_setapprole 'application Role name','password';";
sqlCommand.CommandText += sqlComm;
sqlCommand.CommandTimeout = 300;
sqlCon.Open();
int res = sqlCommand.ExecuteNonQuery();
}
This code is in a loop. for the first time, it's OK. In second iterator, it throws exception.
The connection has been dropped because the principal that opened it subsequently assumed a new security context, and then tried to reset the connection under its impersonated security context. This scenario is not supported. See "Impersonation Overview" in Books Online.
Event ID 18059
Source MSSQLSERVER
Is there anyone meet this problem before?
Best Regards,
turn off connection pooling, explicitly. by default is on and connection pooling does not work with unreversible impersonation like approles.
The using{} statement calls IDispose at the end of the block thus killing your connection.
https://msdn.microsoft.com/en-us/library/yh598w02.aspx
using Statement (C# Reference)

Categories

Resources