Click here to Skip to main content
15,895,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have written a function function , that i want to call using javascript, ajax.

What I have tried:

web page code

HTML
<script type = "text/JavaScript">
       
             function statebind() {
                  $.ajax({
                     type: "POST",
                     url: "TestAjax.aspx.cs/bindstate",
                     data: JSON.stringify({ username: $('#ddlCountry').val() }),
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function (data) 
                     {
                         if (data) 
                        {
                            toastr.success('Congratulations! Username is available.');
                        }
                        else 
                        {
                            toastr.error('Please choose another Username!');  
                            $('#txtUsername').focus();
                        }
                   },
                    error: function (data) 
                   {
                   }
                 });
              }
  </script>


C# code

C#
[System.Web.Services.WebMethod]
       public static void bindstate(string id)
       {
           try
           {
               OracleDataReader dr;
               string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
               OracleConnection conn = new OracleConnection(connectionString);
               conn.Open();
               String Str = "select STATE_ID,STATE_NAME from com_state_master where COUNTRY_ID="+id;
               OracleCommand cmd = new OracleCommand(Str, conn);
               dr = cmd.ExecuteReader();
               conn.Close();
           }
           finally
           {
           }
       }


   }
Posted
Updated 18-Nov-16 17:42pm
v2
Comments
StM0n 16-Nov-16 0:38am    
Are you aware that you open your site for sql injection? the parameter <id> is a plain string, that just get concatenated to a statement.

make it as static method and add WebMethod Attribute to it.
C#
[WebMethod]
public  static void bindstate(string id)
{
}


your code is vulnerable to Sql injection[^] attack, always use Parameterised query to avoid sql injection attacks[^]
 
Share this answer
 
Just declare your bindstate() method as a WebMethod like

C#
[System.Web.Services.WebMethod]
public  void bindstate(string id)
        {
            try
            {
                string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                OracleConnection conn = new OracleConnection(connectionString);
                conn.Open();
                String Str = "select STATE_ID,STATE_NAME from com_state_master where COUNTRY_ID="+id;
                OracleCommand cmd = new OracleCommand(Str, conn);
                ddlState.DataSource = cmd.ExecuteReader();
                ddlState.DataTextField = "COUNTRY_NAME";
                ddlState.DataValueField = "COUNTRY_ID";
                ddlState.DataBind();
                conn.Close();
            }
            finally
            {
            }
        }
 
Share this answer
 
Comments
manish-gusain8909 18-Nov-16 5:49am    
i have tried but its not working

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