Home > Mobile >  insert data into database using javascript in asp.net C# After inserting the data into database,i wa
insert data into database using javascript in asp.net C# After inserting the data into database,i wa

Time:01-25

[WebMethod]

    public static string submitData(string FullName, string Gender, string Age, string Address, string Email, string Contact, string Department, string Position, string Username, string Password)
    {
        string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        SqlConnection con = new SqlConnection(cs);
        con.Open();
        SqlCommand check = new SqlCommand("select count(*) from employee where Email = '"   Email.ToString()
              "' and Contact = '"   Contact.ToString()   "' and Username = '"   Username.ToString()   "'");
        int count = (int)check.ExecuteScalar();

        if (count > 0)
        {
            // Alert

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('employee  Already Exits !.')", true);

            return;
        }
        else
        {
            SqlCommand cmd = new SqlCommand(@"
                insert into employee (FullName, Gender,Age,Address,Email,Contact,Department,Position,Username,Password) 
                values(@FullName,@Gender,@Age,@Address,@Email,@Contact,@Department,@Position,@Username,@Password) 
                ", con);
            cmd.Parameters.AddWithValue("@FullName", FullName);
            cmd.Parameters.AddWithValue("@Gender", Gender);
            cmd.Parameters.AddWithValue("@Age", Age);
            cmd.Parameters.AddWithValue("@Address", Address);
            cmd.Parameters.AddWithValue("@Email", Email);
            cmd.Parameters.AddWithValue("@Contact", Contact);

            cmd.Parameters.AddWithValue("@Department", Department);
            cmd.Parameters.AddWithValue("@Position", Position);
            cmd.Parameters.AddWithValue("@Username", Username);
            cmd.Parameters.AddWithValue("@Password", Password);
            cmd.ExecuteNonQuery();

            return "true";
        }
    } 

now the else part is working fine but the if part isn't working.I'm getting error in alert. so i wanna if employe already regestrated to get message sying already exist.!!

CodePudding user response:

Try using RegisterStartupScript instead. Also, side note, you have a typo in your alert message box. exits != exists :)

 ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('employee  Already Exists !.')", true);

CodePudding user response:

You can't use register client script inside of a web method, since there is no round trip and page cycle occurring.

Register client script requires that the page was posted to the server, then code behind runs, and then the WHOLE web page travels back to the client, is re-loaded, then displayed, and THEN your injected script runs.

however, since this is a web method, then that means client side js code is calling and running the web method, and thus that means the client side script can launch/display/have/enjoy code to launch/display any dialog box or whatever you want to display in that client side js code. So, have the web method return some "yes text" or whatever you want to display, and write that code in the client side js code to then display the correct message or dialog based on what the web method returns.

So, to "inject" or "register" some js code into the web page? That requires that the web page has been posted to the server, and that means a post-back of the web page will have to occur.

The web method can't update controls on the web page, nor inject js scripts, since the page is STILL sitting on the user's desktop, and never made the trip to the server.

however, as noted, since your js code client side will be calling the webmethod, then it should be a simple matter to have such js code client side display that message based on a return value from the webmethod. In your case, you return "true" as a string if the data was to be inserted, and thus in place of a register script, return a "false" string, and have the calling js code then display some message based on that true or false returned to that calling js code.

  • Related