Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接字符串未正确初始化_C#_Sql_Connection String_Sqlconnection - Fatal编程技术网

C# 连接字符串未正确初始化

C# 连接字符串未正确初始化,c#,sql,connection-string,sqlconnection,C#,Sql,Connection String,Sqlconnection,我一直收到一个错误,连接字符串没有正确初始化。我检查了其他答案,但找不到解决方案。我的连接字符串适用于应用程序的其他部分,但不适用于一个部分。通过ondatabound进行调试时,它会在Connection.open处显示错误 <connectionStrings> <add name="SmartFormConnection" connectionString="Data Source=OAK-HRSA-DB01;Initial Catalog=ORHP_Dev;User I

我一直收到一个错误,连接字符串没有正确初始化。我检查了其他答案,但找不到解决方案。我的连接字符串适用于应用程序的其他部分,但不适用于一个部分。通过ondatabound进行调试时,它会在Connection.open处显示错误

<connectionStrings>
<add name="SmartFormConnection" connectionString="Data Source=OAK-HRSA-DB01;Initial Catalog=ORHP_Dev;User ID=blah;Password=blah" providerName="System.Data.SqlClient"/>
</connectionStrings>
二等舱

Sql Connection _connection = DataAccess.SelfRef().GetConnection();


protected void SectionInstructionListBox_OnDataBound(Object sender, EventArgs e)
{

    var ctrl = (Control)sender;
    var panel = (Panel)ctrl.FindControl("NoDataReturnedPanel");
    var listbox = (ListBox)ctrl.FindControl("SectionInstructionListBox");

    var reportdropdownlist = ReportPeriodDropDownList.SelectedValue;
    var programdropdownlist = ProgramDropDownList.SelectedValue;
    var formsectionlistbox = FormSectionListBox.SelectedValue;


    var sectionInstructionTextBox = (TextBox)ctrl.FindControl("SectionInstructionTextBox");

    using (var connection = _connection)
    {
        try
        {
            if (FormSectionListBox.SelectedValue != "" && ReportPeriodDropDownList.SelectedValue != "" && ProgramDropDownList.SelectedValue != "")
            {
                if (connection != null && connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                String selectquery = "Select Instruction from Core.SectionInstruction where FormSectionID = " +
                                         formsectionlistbox + " AND DataCollectionPeriodID = " + reportdropdownlist +
                                         " AND ProgramID = " + programdropdownlist;

                var command = new SqlCommand(selectquery);

                command.Connection = connection;
                command.CommandType = CommandType.Text;
                sectionInstructionTextBox.Text = command.ExecuteScalar().ToString();
                sectionInstructionTextBox.DataBind();
            }
        }
        catch (Exception ex)
        {
            //Display error message
            ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Error: " + ex.Message + "');",
                                                    true);
        }
    }

        panel.Visible = listbox.Items.Count == 0;
        listbox.Visible = listbox.Items.Count != 0;

}

当你的应用程序失败时,连接字符串是什么?@DavidG Ahh此时显示为空point@DavidG让它工作了谢谢你的评论你想允许ConnString从DataAccess外部更改吗?如果不是,则应将其设置为private或readonly-public字符串ConnString{get;private set;}。如果应用程序只是在这里中断,那么可能有什么东西正在改变并中断它。将其设置为只读将显示此信息。@user3339242没问题,我们经常会忘记检查一些小事情:
Sql Connection _connection = DataAccess.SelfRef().GetConnection();


protected void SectionInstructionListBox_OnDataBound(Object sender, EventArgs e)
{

    var ctrl = (Control)sender;
    var panel = (Panel)ctrl.FindControl("NoDataReturnedPanel");
    var listbox = (ListBox)ctrl.FindControl("SectionInstructionListBox");

    var reportdropdownlist = ReportPeriodDropDownList.SelectedValue;
    var programdropdownlist = ProgramDropDownList.SelectedValue;
    var formsectionlistbox = FormSectionListBox.SelectedValue;


    var sectionInstructionTextBox = (TextBox)ctrl.FindControl("SectionInstructionTextBox");

    using (var connection = _connection)
    {
        try
        {
            if (FormSectionListBox.SelectedValue != "" && ReportPeriodDropDownList.SelectedValue != "" && ProgramDropDownList.SelectedValue != "")
            {
                if (connection != null && connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                String selectquery = "Select Instruction from Core.SectionInstruction where FormSectionID = " +
                                         formsectionlistbox + " AND DataCollectionPeriodID = " + reportdropdownlist +
                                         " AND ProgramID = " + programdropdownlist;

                var command = new SqlCommand(selectquery);

                command.Connection = connection;
                command.CommandType = CommandType.Text;
                sectionInstructionTextBox.Text = command.ExecuteScalar().ToString();
                sectionInstructionTextBox.DataBind();
            }
        }
        catch (Exception ex)
        {
            //Display error message
            ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Error: " + ex.Message + "');",
                                                    true);
        }
    }

        panel.Visible = listbox.Items.Count == 0;
        listbox.Visible = listbox.Items.Count != 0;

}