Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

Object not set to a reference

I am getting an error where I wasnt getting one before - kinda puzzling
Object reference not set to an instance of an object. Any help would be appreciated
Thanks
con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if (o != DBNull.Value)
                {
                    UID = (int)o;<-- ERROR HERE!
                    COUNT = 1;
                }
                else
                {
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    UID = (int)com.ExecuteScalar();
                    COUNT = 0;

                }

                con.Close();

Open in new window

Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

In which line youre getting the error??
where and how is UID declared?
Avatar of r3nder

ASKER

Here at the top
if (LogonHelper.VerifyADLogon(txtBxDomain.Text, txtBxUsername.Text, txtBxPassword.Text))
            {
                int COUNT;
                int UID;

                SqlConnection con = new SqlConnection("Data Source=10.10.0.27;Initial Catalog=DB;User ID=User;password=pass;Integrated Security=False;");


                con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                int o = com.ExecuteScalar();
                if (o != DBNull.Value)
                {
                    UID = (int)o;
                    COUNT = 1;
                }
                else
                {
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    UID = (int)com.ExecuteScalar();
                    COUNT = 0;

                }

                con.Close();


                Label1.Visible = true;
                if (COUNT == 0)
                {
                    Label1.Text = "Your login has been authenticated.......Thank you";

                }
                else
                {
                    Label1.Text = "Welcome Back!.." + " " + txtBxUsername.Text;

                }

                ClientScript.RegisterStartupScript(this.GetType(), "ShowLbl", "<script>TimeOutFuc();</script>");
                ClientScript.RegisterStartupScript(this.GetType(), "frame1", "<script>startUp(" + UID.ToString() + ");</script>");
            }
            else

Open in new window

when did object o become int o?

                int o = com.ExecuteScalar();
Which one gave you the error, the question or comment #3?
Avatar of r3nder

ASKER

I was trying something - didnt work
it is back to object o = com.ExecuteScalar();
Avatar of r3nder

ASKER

cyber
What i did was delete myself from the database to let it add me again for final testing
and thats when it threw the error
Avatar of r3nder

ASKER

the question gave the error - not comment
Always test for

                if (o == DBNull.Value)

Because

ExecuteScalar can return one of 3 things
object (if not null and has a result row)
DbNULL.Value (if has a row, but value is SQL null)
null - no result row at all

So you either test
                if ((o != null) && (o != DBNull.Value))
or it is easier and you always see this more common test
                if (o == DBNull.Value)

Just change the if and else branch
Avatar of r3nder

ASKER

that fixed that issue but now I have another
Specified Cast is not valid
con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if (o == DBNull.Value)
                {
                    UID = (int)o;
                    COUNT = 1;
                }
                else
                {
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    UID = (int)com.ExecuteScalar(); <-- ERROR HERE!
                    COUNT = 0;

                }

                con.Close();

Open in new window

Since you reversed the test, the branches were supposed to be moved...

                con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if (o == DBNull.Value)
                {
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    UID = (int)com.ExecuteScalar(); <-- ERROR HERE!
                    COUNT = 0;
                }
                else
                {
                    UID = (int)o;
                    COUNT = 1;
                }

                con.Close();
Avatar of r3nder

ASKER

Both ways throw an error - object reference not set to the instance of an object
//first way
//-------------------------
con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if (o == DBNull.Value)
                {
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    UID = (int)com.ExecuteScalar(); 
                    COUNT = 0;
                }
                else
                {
                    UID = (int)o; <---- ERROR HERE
                    COUNT = 1;
                }

                con.Close();

//Second way
//--------------------

con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if ((o != null) && (o != DBNull.Value))
                {
                    UID = (int)o;
                    COUNT = 1;
                }
                else
                {
                    
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    
                    UID = (int)o; <----- ERROR HERE
                    COUNT = 0;

                }

                con.Close();

Open in new window


con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if (o == DBNull.Value)
                {
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("SET NOCOUNT ON; INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SET NOCOUNT OFF; SELECT SCOPE_IDENTITY()", con);
                    UID = (int)com.ExecuteScalar(); <-- ERROR HERE!
                    COUNT = 0;
                }
                else
                {
                    UID = (int)o;
                    COUNT = 1;
                }

                con.Close();

Open in new window

Avatar of r3nder

ASKER

same
Object reference not set to an instance of an object.
Sorry to be such a bother but your help is appreciated
con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if (o == DBNull.Value)
                {
                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("SET NOCOUNT ON; INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SET NOCOUNT OFF; SELECT SCOPE_IDENTITY()", con);
                    UID = (int)com.ExecuteScalar(); 
                    COUNT = 0;
                }
                else
                {
                    UID = (int)o; <------ERROR
                    COUNT = 1;
                }

                con.Close();

Open in new window

Avatar of r3nder

ASKER

you think I should put another SQL statement in there to get the ID - instead of getting it on the same query?
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r3nder

ASKER

yes that was missing - I will check it in the morning and let you know- I am at home right now. But thanks for hanging in there

r3nder
Avatar of fenix3
fenix3

try
if (o != DBNull.Value && o != null)
Avatar of r3nder

ASKER

this is the error after the change
Specified cast is not valid.

con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if ((o != null) && (o != DBNull.Value))
                {
                    UID = (int)o;
                    COUNT = 1;
                }
                else
                {

                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    o = com.ExecuteScalar();   
                    UID = (int)o; //<--ERROR
                    COUNT = 0;

                }

                con.Close();

Open in new window

Avatar of r3nder

ASKER

Think I got it!!!
con.Open();
                string TxtBxUser = txtBxUsername.Text;
                SqlCommand com = new SqlCommand("SELECT [ID] FROM [USER] WHERE USER_NAME = '" + TxtBxUser + "'", con);

                object o = com.ExecuteScalar();
                if ((o != null) && (o != DBNull.Value))
                {
                    UID = (int)o;
                    COUNT = 1;
                }
                else
                {

                    DateTime ENTERED = DateTime.Now;
                    com = new SqlCommand("INSERT INTO [USER](USER_NAME,LOGIN_DATE) " +
                                         "Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
                    o=com.ExecuteScalar();
                    int.TryParse(o.ToString(), out UID);
                    

                    //UID = (int)o; 
                    COUNT = 0;

                }

                con.Close();

Open in new window