C# 以下连接字符串中有什么错误?

C# 以下连接字符串中有什么错误?,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我正在尝试在asp.net页面中创建页面,出现以下错误 错误:-System.NullReferenceException:对象引用未设置为对象的实例。在TestdateAssistor.user\u info.Button1\u Click1(对象发送者,事件参数e) 在这条线上 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\S

我正在尝试在asp.net页面中创建页面,出现以下错误

错误:-System.NullReferenceException:对象引用未设置为对象的实例。在TestdateAssistor.user\u info.Button1\u Click1(对象发送者,事件参数e)

在这条线上

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\SQLEXPRESS;Integrated Security=True"].ConnectionString);
这是我的完整代码

   try
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"].ConnectionString);
        conn.Open();
        String insert = "insert into Table (NAME,ADDRESS,MOBILE NO,ADHAR NO,DOB) values (@name,@add,@mob,@adhar,@dob)";
        SqlCommand com = new SqlCommand(insert,conn);
        com.Parameters.AddWithValue("@name",TextBox1.Text);
        com.Parameters.AddWithValue("@add",TextBox2.Text);
        com.Parameters.AddWithValue("@mob",TextBox3.Text);
        com.Parameters.AddWithValue("@adhar", TextBox4.Text);
        com.Parameters.AddWithValue("@dob", TextBox5.Text);
        com.ExecuteNonQuery();
        Response.Write("Successful Registration!!");
        conn.Close();
    }

    catch (Exception ex)
    {
        Response.Write("Error:-" + ex.ToString());
    }

我应该对连接字符串进行哪些更改?

您正在使用连接字符串作为Web.config中定义的连接字符串的键。因此,您需要在此处定义连接字符串并为其命名,然后在代码中按名称引用它:

Web.config:

<connectionStrings>
<add name="myConnectionString" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True" />
</connectionStrings>

您正在使用连接字符串作为Web.config中定义的连接字符串的键。因此,您需要在此处定义连接字符串并为其命名,然后在代码中按名称引用它:

Web.config:

<connectionStrings>
<add name="myConnectionString" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True" />
</connectionStrings>

ConnectionString是框架自动为您构建的集合。它的内容是从web.config中检索的,您应该在相应的部分中定义它

然后通过方括号中的
名称
而不是整个connectionstring来检索其值

string cnString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
SqlConnection conn = new SqlConnection(cnString);
并在web.config中为connectionstring添加正确的定义

<configuration>
  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"/>
  </connectionStrings>
  .....
</configuration>

.....

ConnectionString是框架自动为您构建的集合。它的内容是从web.config中检索的,您应该在相应的部分中定义它

然后通过方括号中的
名称
而不是整个connectionstring来检索其值

string cnString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
SqlConnection conn = new SqlConnection(cnString);
并在web.config中为connectionstring添加正确的定义

<configuration>
  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"/>
  </connectionStrings>
  .....
</configuration>

.....
错误:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"].ConnectionString); 
解决方案1:
在主程序(.cs)中

解决方案2:
在web.config中

<configuration>
  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"/>
  </connectionStrings>
</configuration>  
参考:错误:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"].ConnectionString); 
解决方案1:
在主程序(.cs)中

解决方案2:
在web.config中

<configuration>
  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"/>
  </connectionStrings>
</configuration>  

参考:

连接字符串是一个集合。您应该在web.config的相应部分中定义它。然后检索方括号中传递名称的值,而不是整个连接字符串。我的宠物皮的可能副本是正确的异常处理,在发生致命异常后继续执行代码是危险的。这里有两个关于mater I链接的艺术品:| ConnectionString是一个集合。您应该在web.config的相应部分中定义它。然后检索方括号中传递名称的值,而不是整个连接字符串。我的宠物皮的可能副本是正确的异常处理,在发生致命异常后继续执行代码是危险的。这里有两个关于mater I链接的文章:|