Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that registraters a new user. When the user puts in their email address and password then click submit I get an error for: System.NullReferenceException {"Object reference not set to an instance of an object."}. What is this error and how can I fix it? The error happens on line 29. that line is:
C#
int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());


Here is all of the code for the form:

C#
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;
using System.Web.Security;
using System.Security.Cryptography;

public partial class SubmitPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();

            string cmdStr = "Select INST_ID, EmailAddress from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            string cmdStr2 = "Select INST_ID, EmailAddress from TableCEO where EmailAddress ='" + TextBoxEA.Text + "'";
            string cmdStr3 = "Select INST_ID, EmailAddress from TableIALO where EmailAddress ='" + TextBoxEA.Text + "'";
            
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select INST_ID, EmailAddress from TableSecurity", con);
            SqlCommand cmd2 = new SqlCommand("select INST_ID, EmailAddress from TableCEO", con);
            SqlCommand cmd3 = new SqlCommand("select INST_ID, EmailAddress from TableIALO", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 1)

                Response.Write("User Name Already Exist!!!<br /> Please Choose Another User Name.");
        }



    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();

        string chkUser = "select INST_ID, EmailAddress from TableCEO where EmailAddress'" + TextBoxEA.Text + "";
        chkUser = "select INST_ID, EmailAddress from TableIALO where EmailAddress'" + TextBoxPW.Text + "";
        SqlCommand chkUsercmd = new SqlCommand(chkUser, con);


        string insCmd = "Insert into TableSecurity (INST_ID, EmailAddress, Password) values (@INST_ID, @EmailAddress, @Password)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        


        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write("An error occurred: " + er.Message);
        }
        finally
        {
        }
    }
    
}
Posted

You are Good Programmer....

But..there is One thing I noticed.... that....you just missed a single line to add into your code while passing Parameters to the SqlCommand Object.



C#
//Your Code....

string insCmd = "Insert into TableSecurity (INST_ID, EmailAddress, Password) values (@INST_ID, @EmailAddress, @Password)";
SqlCommand insertUser = new SqlCommand(insCmd, con);



//Here is the Solution--
//Add @INST_ID to SQLCommand

        insertUser.Parameters.AddWithValue("@INST_ID", 'TextBoxName.Text');

//--------------------

      //Your Code...
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
 
Share this answer
 
v2
Comments
Computer Wiz99 15-Oct-13 11:17am    
Thanks for the compliment MayurDighe. Even the best of us will miss something every once in a while. It just takes another pair of eyes to look at it again.

There is only one problem with the code you gave me. The @INST_ID does not have a textbox to enter that field. I am getting that data from the database. Is there a different way for me to get the INST_ID from one table to another depending on the email address of that user?
thatraja 15-Oct-13 11:25am    
Just pass the value which has INST_ID value.
if variable strINST_ID has value then write like below.
insertUser.Parameters.AddWithValue("@INST_ID", strINST_ID);
Computer Wiz99 15-Oct-13 11:39am    
Ok. This is TableSecurity lay out.

INST_ID
EmailAddress
Password
accessLevel

All of my other Tables have almost the samething with added data fields. All of my Tables has INST_ID in them because of the data that matches the user. I have all of the users profile in CEO Table that starts with the INST_ID. I am trying to get that ID to the security table where the password and emailaddress are.
Yup....

Fetch the value of INST_ID from required table...and just assign it to the local variable ...then pass its value instead of Textbox into SqlCommand object


C#
int tempID='code for retrieving unique INST_ID'

insertUser.Parameters.AddWithValue("@INST_ID", tempID);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900