1

My code gets to conn.Open and gives me "Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll"

Here is the block of code:

        _timer.Stop();
        string path = @"C:\testlog.log";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
        string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open(); // Dies here
        SqlDataReader rdr = cmd.ExecuteReader();
2
  • Share with us the full error message and stack trace. Commented Oct 14, 2016 at 15:42
  • Write access to the root will be denied under usual circumstances. Commented Oct 14, 2016 at 15:42

2 Answers 2

1

Order is missing ...first open connection then use sql command

_timer.Stop();
        string path = @"C:\testlog.log";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
        conn.Open(); // sholud be here

        string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
        SqlCommand cmd = new SqlCommand(query, conn);
        SqlDataReader rdr = cmd.ExecuteReader();

Besides your code is not formatted either..Format like this

 string path = @"C:\testlog.log";
            String connectionString = ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open(); // sholud be here

                string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
                SqlCommand cmd = new SqlCommand(query, conn);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                { 
                    String RawImportEnabled = Convert.ToString(reader["RawImportEnabled"]);
                    //Do some thing
                }
            }

This has some benefits like debugging the connection string by putting a breakpoints ,Disposal of connection without worrying for using statements etc

Sign up to request clarification or add additional context in comments.

2 Comments

Tried modifying the code, still hangs at conn.Open() Let me post modified code as answer so it formats right.
Check your connection string with my code by putting a break point and run with debugging
0
            _timer.Stop();
        string path = @"C:\testlog.log";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
        string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
        conn.Open(); // Dies here again.
        SqlCommand cmd = new SqlCommand(query, conn);

        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)

1 Comment

Never mind, it is a connection error, cannot use trusted, must use SQL Server Connection User. Thanks all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.