Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to use multiple client side validation for both email and password in asp.net c# ?


What I have tried:

i tried this but

private Boolean checkemail()
       {
           SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
           Boolean emailavailable = false;
           String myquery = "Select * from [test].[dbo].[MYFORM] where email='" + TXTEmail.Text + "'";


           SqlCommand cmd = new SqlCommand();
           cmd.CommandText = myquery;
           cmd.Connection = conn;
           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = cmd;
           DataSet ds = new DataSet();
           da.Fill(ds);
           if (ds.Tables[0].Rows.Count > 0)
           {
               emailavailable = true;
           }
           conn.Close();
           return emailavailable;
       }

       private Boolean checkMobile()
       {
           SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
           Boolean mobile = false;
           String myquery = "Select * from [test].[dbo].[MYFORM] where contact='" + TXTContact.Text + "'";

           SqlCommand cmd = new SqlCommand();
           cmd.CommandText = myquery;
           cmd.Connection = conn;
           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = cmd;
           DataSet ds = new DataSet();
           da.Fill(ds);
           if (ds.Tables[0].Rows.Count > 0)
           {
               mobile = true;
           }
           conn.Close();
           return mobile;
       }
Posted
Updated 24-Feb-20 18:35pm

1 solution

First, never build a SQL script by concatenating strings. Use parameterized queries instead.
How to Fix SQL Injection Using Microsoft .Net Parameterized Queries[^]
Create Parameterized Queries in ADO.NET -- Visual Studio Magazine[^]

Second, why doing a SELECT * when you could simply do a SELECT COUNT(id)? Why selecting all columns when you only need a single value?

Third, you do not need to fill a datatable just for the sake of counting its elements. If you follow second advise, all you have to do is call ExecuteScalar on your command object. Plus, your actual logic is wrong; if the count is greater than zero, the email is NOT available. Maybe you intended to check whether email exists instead?

Example with first method:
C#
private bool CheckEmail()
{
   int count = 0;
   using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
   {
      conn.Open();
      String myquery = "SELECT COUNT([id]) from [test].[dbo].[MYFORM] WHERE [email]=@email";

      using (SqlCommand cmd = new SqlCommand(myquery, conn))
      {
         cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = TXTEmail.Text;
         try
         {
            count = cmd.ExecuteScalar();
         }
         catch (Exception ex)
         {
            // Log ex.Message + ex.StackTrace
         }
      }
   }
   bool emailExists = count > 0;
   bool emailAvailable = count == 0;
   return emailExists; // or emailAvailable
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900