Re: What to do with a Null Value that is returned from a Select. ??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Cowboy \(Gregory A. Beamer\) (NoSpamMgbworld_at_comcast.netNoSpamM)
Date: 03/19/04


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" />


Relevant Pages

  • Re: What to do with a Null Value that is returned from a Select. ??
    ... do yourself a favor and get rid of the dynamic SQL. ... but I dont seem to be able catch it properly. ... > SqlConnection myConnection = new SqlConnection ... > The HTML is simple and just contains a text box. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: PSP
    ... % Suciu and Pusztai ... Furthermore, if a query fails, it does not ... look like it allows the programmer to directly mix the Prolog and HTML. ...
    (comp.lang.prolog)
  • [NEWS] IBM Informix Web DataBlade Vulnerability Allows SQL Injection
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... When a user makes a page request, webdriver executes a query that will ... This query is vulnerable to SQL injection ... returns some processed HTML. ...
    (Securiteam)
  • Re: multi table multi database join?
    ... dont i have some kind of filter? ... sybase runns on one machine mssql on another, ... the data from the aggregate db could be used in a query to ...
    (microsoft.public.dotnet.framework.adonet)
  • IBM Informix Web DataBlade: SQL injection
    ... SQL code is executed under the uid that webdriver connects as. ... This query is vulnerable to SQL ... processed HTML. ...
    (Bugtraq)