Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two Tables in one database. I have a form that works like this. The user can put in an email address and password then confirm password then click the submit button and be redirected back to the login screen. The code I have is not working. I am selecting the right fields from the table and inserting them into a new table. Why is this not working? Table1 has the data already in it. Table2 is where I want to save the data to. So, if Table1 has INST_ID, Firstname, LastName, Emailaddress, Phone Number. I am selecting the INST_ID and Emailaddress from Table1 and inserting it into Table2 with the password. It will not insert into Table2, Why?

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 == 0)

                lblMessage.Text = "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);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        


        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write("An error occurred: " + er.Message);
        }
        finally
        {
        }
    }
    
}
Posted
Comments
mgoad99 16-Oct-13 16:30pm    
What happens when you click submit? Do you get an error message, does it redirect to the login page?
Computer Wiz99 16-Oct-13 19:48pm    
The error message is on line 29 which is: int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
Error Message:

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

Harshil_Raval 17-Oct-13 0:26am    
I must say, your coding pattern is very bad.. Why you are checking whether user is exist or not on every postback event? see in page_Load method you have if condition if(Postback)!!!!!! Other thing, your sql command userExist is having select statement, and still you are using ExecuteScalar to get results!!!!! Also, in Page_Load event you have open connection, but no con.Close()!!!!!!! First correct these errors....
MTProgrammer 16-Oct-13 17:43pm    
Hmmm your code is a little confusing for instance, in the Page_Load event you have defined three strings each with its own sql but you only ever execute the first one, cmdStr; the other two are never executed.

For this functionality I would write a sql stored procedure that checks all three tables for the specified TextBoxEA.Text value and have it return a bit (0 if not found 1 if found) then you can modify your code to execute the sproc instead of the tsql select statements.

In your button click event you have more confusing code, for instance you define a SqlCommand, chkUsercmd, but never execute the SqlCommand?!...!?!...

YOu may want to clean all this up and then repost.

1 solution

Insert into table_1 select * from table_2;
 
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