Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ExecuteScalar() method

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
hi i having trouble with ExecuteScalar(),

[1]I getting a null reference exception that was un handled by me.

[2] use the keyword 'new' to create an object instance, but when i add new i get:
Object reference not set to an instance of an object.

Here are some lines of code, any suggestions, thanks:

Command.Parameters.Add("@pass", SqlDbType.NVarChar).Value = strPassword;

connection.Open();

int va1 = (int)Command.ExecuteScalar();

Console.WriteLine(va1);

connection.Close();
 
Hi,
I'm not sure I understand your question, but I will just like to make a comment on your sample code. It is unsafe to do an immediate cast against ExecuteScalar(). Instead, store the value on an object type var and test it. There are 3 conditions to watch: 1) no record returned, 2) a record is returned but the value is a NULL, 3) data received.
Code:
[COLOR=green]/* Configure command object */[/color]
SqlConnection con = ...;
SqlCommand cmd = ...;

con.Open();
object data = cmd.ExecuteScalar();
con.Close();

if (data == null)
	MessageBox.Show("No record received.");
else if (data is DBNull)
	MessageBox.Show("Record received. Value is DBNull.");
else
	MessageBox.Show("Data received = " + data.ToString());

hope this helps [wink]
 
phinoppix, thank for the pointers, i'm having problems showing the message, the code is in LoginForm.acsx.cs

and i'm including the LoginForm into a page called memberLogin, like so:

<%@ Register Src="userControls/LoginForm.ascx" TagName="LoginForm" TagPrefix="uc1" %>

And its in the memberLogin.aspx page i have a place holder for msg

Code:
if (data == null)
     Msg.Txt("No record received.");
else if (data is DBNull)
     Msg.Txt("Record received. Value is DBNull.");
else
     Msg.Txt("Data received = " + data.ToString());

Msg does not exist in the current context, intellisense gave me the option of:
WebApplication1.member_area.MemberLogin.Msg.txt...

but still not referenced correctly? any suggestions, thanks
 
Not sure what you're trying to do, but why not use Response.Write to show the messages?

If you want to have a message box, here is a function I use in a separate class to put message boxes on my page.

Code:
    public static void CreateMsgBox(System.Web.UI.Page aspxPage, string strMessage, string strKey)
    {
        string strScript = "<script language = JavaScript>alert('" + strMessage + "')</script>";
        if (!aspxPage.ClientScript.IsStartupScriptRegistered(strKey))
        {
            aspxPage.ClientScript.RegisterStartupScript(aspxPage.GetType(), strKey, strScript);
        }
    }

Hope it helps,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top