r3nder
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
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();
In which line youre getting the error??
where and how is UID declared?
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
when did object o become int o?
int o = com.ExecuteScalar();
Which one gave you the error, the question or comment #3?
int o = com.ExecuteScalar();
Which one gave you the error, the question or comment #3?
ASKER
I was trying something - didnt work
it is back to object o = com.ExecuteScalar();
it is back to object o = com.ExecuteScalar();
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
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
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
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
ASKER
that fixed that issue but now I have another
Specified Cast is not valid
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();
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_DAT E) " +
"Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
UID = (int)com.ExecuteScalar(); <-- ERROR HERE!
COUNT = 0;
}
else
{
UID = (int)o;
COUNT = 1;
}
con.Close();
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_DAT
"Values ('" + TxtBxUser + "','" + ENTERED + "'); SELECT SCOPE_IDENTITY()", con);
UID = (int)com.ExecuteScalar(); <-- ERROR HERE!
COUNT = 0;
}
else
{
UID = (int)o;
COUNT = 1;
}
con.Close();
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();
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();
ASKER
same
Object reference not set to an instance of an object.
Sorry to be such a bother but your help is appreciated
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();
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
r3nder
try
if (o != DBNull.Value && o != null)
if (o != DBNull.Value && o != null)
ASKER
this is the error after the change
Specified cast is not valid.
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();
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();