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