C# NullReferenceException我对它们很熟悉,但在这种情况下无法解决
我对ASP.net相当陌生,但我熟悉空引用异常,但我无法解决这个问题。我试图从webform收集数据,并通过连接字符串将其传递到我的数据库,这时会发生异常C# NullReferenceException我对它们很熟悉,但在这种情况下无法解决,c#,asp.net,sql,nullreferenceexception,sqlconnection,C#,Asp.net,Sql,Nullreferenceexception,Sqlconnection,我对ASP.net相当陌生,但我熟悉空引用异常,但我无法解决这个问题。我试图从webform收集数据,并通过连接字符串将其传递到我的数据库,这时会发生异常 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; usin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (IsPostBack)
{
SqlConnection studConnA = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnection"].ConnectionString);
studConnA.Open();
string checkuser = "select count(*) from StudTable where Name='" + TextBoxName.Text + "'";
SqlCommand studComA = new SqlCommand(checkuser, studConnA);
int temp = Convert.ToInt32(studComA.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User already Exists");
}
studConnA.Close();
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
protected void Button1_Click(object sender, System.EventArgs e)
{
try
{
SqlConnection studConn = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnectionString"].ConnectionString);
studConn.Open();
string insertQuery = "insert into StudTable (Name,Email,Age,Continent,School,Password) values (@name,@email,@age,@cont,@school,@pass)";
SqlCommand studCom = new SqlCommand(insertQuery, studConn);
studCom.Parameters.AddWithValue("@name", TextBoxName.Text);
studCom.Parameters.AddWithValue("@email", TextBoxEmail.Text);
studCom.Parameters.AddWithValue("@age", TextBoxAge.Text);
studCom.Parameters.AddWithValue("@cont",DropDownCont.SelectedItem.ToString());
studCom.Parameters.AddWithValue("@school", TextBoxSchool.Text);
studCom.Parameters.AddWithValue("@pas", TextBoxPass.Text);
studCom.ExecuteNonQuery();
Response.Redirect("Backend.aspx");
Response.Write("Your Registration is Sucessful");
studConn.Close();
}
catch (Exception ex)
{
Response.Write("Error:" +ex.ToString());
}
}
}
空引用出现在第19行
Line 17: if (IsPostBack)
Line 18: {
Line 19: SqlConnection studConnA = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnection"].ConnectionString);
Line 20: studConnA.Open();
Line 21: string checkuser = "select count(*) from StudTable where Name='" + TextBoxName.Text + "'";
我相信问题在于我的连接字符串的语法,但我不确定
有人能帮我解决这个问题吗?似乎没有配置名为StudConnection的连接字符串。检查您的配置文件以获取以下密钥:
<connectionStrings>
<add name="StudConnection" connectionString="YOUR DETAILS" />
</connectionStrings>
听起来好像你没有一个名为
StudConnection
的连接字符串-尽管即使你已经解决了这个问题,你也应该参数化你的SQL,而不是像现在这样直接在其中包含值。这个nullref和其他的一样。您已取消对空引用的引用。点之前的一个东西是空的。可能是,ConfigurationManager.ConnectionStrings[“StudConnection”]
。谢谢你的帮助,我不知怎么导入了错误的web.config,但现在我解决了它!
if (IsPostBack)
{
// if this line fails, then you don't have the proper connection string
// element in the config file.
Debug.Assert(ConfigurationManager.ConnectionStrings["StudConnection"] != null);
SqlConnection studConnA = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnection"].ConnectionString);
studConnA.Open();
string checkuser = "select count(*) from StudTable where Name='" + TextBoxName.Text + "'";
SqlCommand studComA = new SqlCommand(checkuser, studConnA);
int temp = Convert.ToInt32(studComA.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User already Exists");
}
studConnA.Close();
}