[Solved] SQL Statement Difference Between C# and JS [closed]


One thing you should know that connecting to your database directly with javascript is a bad practice due to many reasons. Here is how you should connect to the database using jQuery:

First change will be to copy the lines of code you have into a method and decorate the method with [WebMethod] attribute. This will be called via ajax to process the request via client side. e.g

[WebMethod]
public static void DoSomething()
{
   string email = string.Empty;
   SqlCommand cmdFindInfo = new SqlCommand("SELECT Email FROM iDen_Login WHERE(Email=@email)", conLogin);
   cmdFindInfo.Parameters.AddWithValue("@email", txtbxMembEmail.Value);
   SqlDataReader rdr = cmdFindInfo.ExecuteReader();
   while (rdr.Read())
   {
      email = rdr["Email"].ToString();
   }
   rdr.Close();
}

then in your markup page you can call this method like below:

$.ajax({
    method: 'POST',
    url: 'MyPage.aspx/DoSomething',
    accept: 'application/json',
    contentType: 'application/json; charset=utf-8',
    success: function(){
       console.log('success');
    },
    fail: function(err){
       console.log(err);
    }
});

Above is an example that will help you connect to sql server with ajax calls. BUT if you strictly want to connect to the sql directly and not through C# at all, then I would recommend you go through this answer.

Above answer gives a way to connect to the Database but this method will put you on great security risks, So it is a bad practice to go this way.

Happy Coding!

solved SQL Statement Difference Between C# and JS [closed]