L
L
Lemeshev Andreevich2020-04-23 11:52:18
C++ / C#
Lemeshev Andreevich, 2020-04-23 11:52:18

ConnectionString property not initialized how to fix?

decided to connect to the database through the class and, as usual, new errors System.InvalidOperationException: "ExecuteScalar needs an open and available connection Connection. The connection is closed."

using (Connction.sqlConnection)
        {
            SqlCommand sqlCommand = new SqlCommand("SELECT Key_num FROM Users WHERE Phone = @Phone", Connction.sqlConnection);
            sqlCommand.Parameters.AddWithValue("@Phone", phone);
            return (int?)sqlCommand.ExecuteScalar();
        }

i added Connction.sqlConnection.Open(); and threw another error : System.InvalidOperationException: "ConnectionString property not initialized."

help me decide)
here is the code of the db connection class
public static class Connction
{
    public static SqlConnection sqlConnection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=D:\MDK\MDK\MDK\DB.mdf;Integrated Security = True");
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-04-23
@yarosroman

Write ConnetionString to appname.exe.config

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question