Re: What to do with a Null Value that is returned from a Select. ??
From: Cowboy \(Gregory A. Beamer\) (NoSpamMgbworld_at_comcast.netNoSpamM)
Date: 03/19/04
- Next message: Cowboy \(Gregory A. Beamer\): "Re: possibly simple ADO/Access question"
- Previous message: XmlAdoNewbie: "dataset and databindings"
- In reply to: Harry: "What to do with a Null Value that is returned from a Select. ??"
- Next in thread: William Ryan eMVP: "Re: What to do with a Null Value that is returned from a Select. ??"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 19 Mar 2004 10:26:30 -0600
SELECT * FROM dbo.Email_List WHERE Email='mine@email.com'
UserID Email
------- -------
1099 mine@email.com
2928 mine@email.com
With your logic, it would return 1099 or NULL. This is not a DBNull issue,
but a misunderstanding of how ExecuteScalar works. If you are simply looking
for a number try this for your SQL:
SqlCommand myCommand1 = new SqlCommand("SELECT COUNT(*) FROM dbo.Email_List
WHERE Email = '" + email.Text + "'" , myConnection);
int userCount = (int)myCommand1.ExecuteScalar();
Execute Scalar returns all from the table and then only pulls the first
value.
It will then return 0 for no records or N (where N is the number of records)
when records are found.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
**********************************************************************
Think Outside the Box!
**********************************************************************
"Harry" <harrysingh1976@hotmail.com> wrote in message
news:1c234377.0403190814.157a03b7@posting.google.com...
> Hi,
>
> Can anyone help with this please?
>
> I have the below code, which I want tp check if an email address
> exsists in my SQL database. It works fine if the email address is
> found, but if the email address does not exsist, it chucks out the
> following error:-
> "System.NullReferenceException: Object reference not set to an
> instance of an object"
>
> No I understand this is because Select query is coming back with a
> 'null' value, but I dont seem to be able catch it properly.
> (Ive read lots about DBNull.Value, but i dont seem to be able to use
> it without getting errors!)
>
> Any help apriciated. (Also, is the below method an 'efficient' way of
> carrying out this type of function?)
>
> Many Thanks in advance
> H
>
>
> void Send_Email(object sender, System.EventArgs e)
> {
> SqlConnection myConnection = new SqlConnection
> System.Configuration.ConfigurationSettings.AppSettings["pdSQL"]);
> myConnection.Open();
> //Run Query to compare the Email Address
> SqlCommand myCommand1 = new SqlCommand("SELECT * FROM dbo.Email_List
> WHERE Email = '" + email.Text + "'" , myConnection);
> int userCount = (int)myCommand1.ExecuteScalar();
>
> if (userCount = 1 )
> {
> Response.Write("Email Matched" );
> }
> else {
> Response.Write("Email NOT Matched");
> }
>
> myConnection.Close();
> }
>
>
>
> HTML
> The HTML is simple and just contains a text box.
> <asp:textbox CssClass="ENEWStextBox" ID="email" runat="server"
> TextMode="SingleLine" />
- Next message: Cowboy \(Gregory A. Beamer\): "Re: possibly simple ADO/Access question"
- Previous message: XmlAdoNewbie: "dataset and databindings"
- In reply to: Harry: "What to do with a Null Value that is returned from a Select. ??"
- Next in thread: William Ryan eMVP: "Re: What to do with a Null Value that is returned from a Select. ??"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|