Question

im fairly new to ASP.net but i am familiar with Null reference exceptions however i cant solve this one. Im trying to collect data from a webform and pass it to my database through a connection string, this is when the exception occurs.

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());
        }
    }
}

The null reference occurs at line 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 + "'";

I believe the issue is in the syntax of my connection string but im not sure,

Can anyone help me to solve this?

Was it helpful?

Solution

Check your configuration file for the following key:

<connectionStrings>
 <add name="StudConnection" connectionString="YOUR DETAILS" />
</connectionStrings>

If it doesn't exist, add the right key and retry.

You can also check the issue with the following code:

   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();
    }

OTHER TIPS

It would appear that there is no connection string named StudConnection configured.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top