Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an add a new user page that is not working right. Can someone check the code to see where I went wrong? It should get the data from the tables I have listed and insert them into the Security table. When I check the security table the user is not in there. It should have UserID, Emailaddress, Password, accesslevel. None are which in the security table when you try to add a new user.

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 EmailAddress from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            string cmdStr2 = "Select EmailAddress from TableCEO where EmailAddress ='" + TextBoxEA.Text + "'";
            string cmdStr3 = "Select EmailAddress from TableIALO where EmailAddress ='" + TextBoxEA.Text + "'";
            
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select EmailAddress from TableSecurity", con);
            SqlCommand cmd2 = new SqlCommand("select EmailAddress from TableCEO", con);
            SqlCommand cmd3 = new SqlCommand("select 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 EmailAddress from TableCEO where EmailAddress'" + TextBoxEA.Text + "";
        chkUser = "select EmailAddress from TableIALO where EmailAddress'" + TextBoxPW.Text + "";
        SqlCommand chkUsercmd = new SqlCommand(chkUser, con);


        string insCmd = "Insert into TableSecurity (EmailAddress, Password) values (@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
        {
        }
    }
    
}
Here is the error that comes up:

 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   SubmitPage.Page_Load(Object sender, EventArgs e) in C:\Users\khopkins\Documents\Visual Studio 2010\Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\SubmitPage.aspx.cs:29
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207


It seem like the error is coming from line 29.

C#
 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 27:             SqlCommand cmd2 = new SqlCommand("select EmailAddress from TableCEO", con);
Line 28:             SqlCommand cmd3 = new SqlCommand("select EmailAddress from TableIALO", con);
Line 29:             int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
Line 30:             if (temp == 1)
Line 31: 


Source File: C:\Users\khopkins\Documents\Visual Studio 2010\Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\SubmitPage.aspx.cs    Line: 29
Posted
Updated 14-Oct-13 3:43am
v3
Comments
thatraja 14-Oct-13 9:28am    
Any error? share it
Computer Wiz99 14-Oct-13 9:33am    
I added the error in the code.
thatraja 14-Oct-13 9:37am    
In which line it occurs?
Ganesh KP 14-Oct-13 9:38am    
Hi Hopkins, what value u r getting in TextBoxEA.Text value, just put a breakpoint and check whether you are getting any values before u insert and also please paste the error you have got, so that it is easier to others to debug and get u the answer so easily.

1 solution

C#
int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());

In the above line userExist.ExecuteScalar() will return the value of first row and first column. Suppose the command returns an e-mail address "abc@xyz.com". Can it be converted into an integer? And if it does not return any row, how can that result be converted into an integer? You have to handle these things gracefully. Use a count in your query. For example:
C#
string cmdStr = "Select count(EmailAddress) from TableSecurity as result where EmailAddress='" + TextBoxEA.Text + "'";

You might escape the error but still, your approach is not correct. Start using stored procedure and check the conditions in the back-end.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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