using SQL in ASP.NET

  • Thread starter Thread starter Bernie V
  • Start date Start date
B

Bernie V

Hi group,

For the first time I have to use SQL with ASP.NET.
I have the following:

a db in SQL called newsletter where I have:

name
surname
emailadress (= the key)

On an aspx form I have 3 fiels on it (name, surname and email)

I want to check if the emailadress on the form is in the db.

Has someboday a clue how to do this ?

grz

Bernie V
 
Hi,

this is trivial task.

write code to handle button click event. on that code open connection to
DB and create SQL query to check if the E-mail value exist in DB :

private void Button1_Click(object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlConnection oCon;
System.Data.SqlClient.SqlCommand oCmd;
try
{
oCon = new System.Data.SqlClient.SqlConnection("Your connection
string to DB");
oCon.Open();
oCmd = new System.Data.SqlClient.SqlCommand("select emailadress from
newsletter where emailadress = '" + email.Text + "'",oCon);
object strRetVal = oCmd.ExecuteScalar ();
if ( strRetVal == null)
{
//mail isnt exist in DB
}
}
catch
{
}
finally
{
if (oCmd != null)
oCmd.Dispose ();
if (oCon.State != System.Data.ConnectionState.Closed)
oCon.Close();
if (cCon != null)
oCon.Dispose ();
}

}


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
Natty Gur said:
Hi,

this is trivial task.

write code to handle button click event. on that code open connection to
DB and create SQL query to check if the E-mail value exist in DB :

private void Button1_Click(object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlConnection oCon;
System.Data.SqlClient.SqlCommand oCmd;
try
{
oCon = new System.Data.SqlClient.SqlConnection("Your connection
string to DB");
oCon.Open();
oCmd = new System.Data.SqlClient.SqlCommand("select emailadress from
newsletter where emailadress = '" + email.Text + "'",oCon);
object strRetVal = oCmd.ExecuteScalar ();
if ( strRetVal == null)
{
//mail isnt exist in DB
}
}
catch
{
}
finally
{
if (oCmd != null)
oCmd.Dispose ();
if (oCon.State != System.Data.ConnectionState.Closed)
oCon.Close();
if (cCon != null)
oCon.Dispose ();
}

}


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377



thx !
I give it a try,

grz

Bernd
 
Back
Top