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

LVL 6
r3nderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jesus RodriguezIT ManagerCommented:
In which line youre getting the error??
Éric MoreauSenior .Net ConsultantCommented:
where and how is UID declared?
r3nderAuthor Commented:
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

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

cyberkiwiCommented:
when did object o become int o?

                int o = com.ExecuteScalar();
Which one gave you the error, the question or comment #3?
r3nderAuthor Commented:
I was trying something - didnt work
it is back to object o = com.ExecuteScalar();
r3nderAuthor Commented:
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
r3nderAuthor Commented:
the question gave the error - not comment
cyberkiwiCommented:
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
r3nderAuthor Commented:
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

cyberkiwiCommented:
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();
r3nderAuthor Commented:
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

cyberkiwiCommented:

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

r3nderAuthor Commented:
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

r3nderAuthor Commented:
you think I should put another SQL statement in there to get the ID - instead of getting it on the same query?
cyberkiwiCommented:
               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();   /// << was this missing?
                    UID = (int)o; // <----- ERROR HERE
                    COUNT = 0;

                }

                con.Close();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r3nderAuthor Commented:
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
fenix3Commented:
try
if (o != DBNull.Value && o != null)
r3nderAuthor Commented:
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

r3nderAuthor Commented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.