如何修复connectionstring属性尚未初始化的错误 [英] How do I fix the connectionstring property has not been initialized error

查看:115
本文介绍了如何修复connectionstring属性尚未初始化的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布我的网站后,我尝试登录该应用程序。登录后我得到了ConnectionString属性尚未初始化错误。



我尝试过的事情:



After publishing my site I attempt to login to the application. After logging in I get the The ConnectionString property has not been initialized error.

What I have tried:

public partial class ComplainantLogin : System.Web.UI.Page
    {
        private static string CS = ConfigurationManager.ConnectionStrings["E-Docket1ConnectionString"].ConnectionString;
        private DataTable dt;







protected void LogIn(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(CS))
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM [COMPLAINANT] WHERE ComplainantID='" + txtIDNum.Text + "' AND Password='" + txtPassword.Text + "'", con))
            {
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                dt = new DataTable();
                sda.Fill(dt);

                if (dt.Rows.Count != 0)//if supplied ID is found
                {
                    //Response.Redirect("Default.aspx");
                    string display = "Message Pop-up!";
                    ScriptManager.RegisterStartupScript(this, GetType(),
                      "ServerControlScript", display, true);
                    string id = txtIDNum.Text;
                    Session["IDNumber"] = id;
                    Response.Redirect("/ComplainantHomePage.aspx");
                    Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Officer ID found');", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Officer ID not found');", true);
                }

            }
        }





和我的web.config





and in my web.config

    <add name="E-Docket1ConnectionString" connectionString="Data Source=SQL7001.myWindowsHosting.com;Initial Catalog=DB_A2C4A1_EDocket; providerName="System.Data.SqlClient" />
</connectionStrings>

推荐答案

请,有这里有很多不妥之处:

1)不要像这样进行数据库访问!永远不要连接字符串来构建SQL命令。它会让你对意外或故意的SQL注入攻击敞开大门,这可能会破坏整个数据库。使用参数化查询代替。



连接字符串时,会导致问题,因为SQL收到如下命令:

Please, there are many things wrong here:
1) Don't do database access like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。您是否定期进行备份,不是吗?



2)切勿以明文形式存储密码:这是一个主要的安全风险。以下是有关如何执行此操作的一些信息:密码存储:如何做到这一点。 [ ^ ]



3)我们无法告诉你为什么代码不能正常工作:你需要使用调试器来在应用程序运行时,看看你从config.web文件中读到的确切内容 - 但我首先将另一个双引号放入:

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text: it's a major security risk. ere is some information on how to do it here: Password Storage: How to do it.[^]

3) We can't tell you why that code doesn't work as is: you need to use the debugger to look at exactly what you have read from your config.web file while the app is running - but I'd start by putting another double quote in:

<add name="E-Docket1ConnectionString" connectionString="Data
Source=SQL7001.myWindowsHosting.com;Initial Catalog=DB_A2C4A1_EDocket";
                                                                     ^
                                                                     | 
                                                                     add this.
 providerName="System.Data.SqlClient" />
</connectionStrings>


这篇关于如何修复connectionstring属性尚未初始化的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆