-6

I have a working C# sql statement :

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();

C# in ASP.NET is the server-side, so I was wondering if this code would be relatively the same using JS for the client-side

  • So, you presumably have an ASP.NET web application, which means you have a full-fledged backend - there's absolutely no valid reason to access your database from your frontend, if only because 1) it throws any security out of the window 2) this isn't what frontend is for. (And your sample code involves getting login information from the database, which makes it twice as bad an idea). Seriously, please reconsider that. – Darth Veyda Mar 14 '19 at 23:54
  • @DarthVeyda what I am trying to do, is not necessarily getting emails, but that was provided as an example query. I am trying to get the total of a selected item from a database. Onserverchange is only whenever you webpage reloads which is not rather want it on the client side. – Alex Bailey Mar 15 '19 at 00:28
  • Onserverchange - so WebForms then. I feel for you - I used to maintain a huge WebForms app where it was impossible to sneeze without posting back the whole page. Then the answer @JamshaidKamran posted is your best bet - you can either add a [WebMethod] or a WebAPI method (https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with-aspnet-web-forms) and make an ajax call to it from client side. SQL from Javascript still isn't the way to go - most if not all solutions are for NodeJS (i.e. _server-side_ Javascript). Good luck! – Darth Veyda Mar 15 '19 at 00:54

2 Answers2

2

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!

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
1

Sorry, but you probably have the wrong question. On the client side, there can be no database queries. Unless you use WebSQL with the full creation of tables and then you can make queries. Well, or the worst option, form the query string to the database and send it to the server where the call will be (very insecure), in which case the query itself will not be different in the syntax. All the simplest SQL queries in all programming languages are no different.

PetrS
  • 51
  • 1