2

I want to display some fields from database using jquery.this is the code for connecting database and display

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnsearch').click(function () {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: "{ CustomerID: '" + $('#txtid').val() + "'}",
                url: "Customer.aspx/FetchCustomer",
                dataType: "json",
                success: function (data) {
                    var Employee = data.d;
                    $('#CustomerDetails').append
                      ('<p><strong>' + Employee.Id + "</strong><br />" +
                      Employee.fname + "<br />" +
                      Employee.lname + "<br />" +
                       "</p>")
                }
            });
        });
    });
</script>

.cs code

[WebMethod]
    public Employee FetchCustomer(string employeeId)
    {
        Employee c = new Employee();
        SqlConnection con = new SqlConnection("Data Source=BAIJU-PC;Initial      Catalog=Baiju;Integrated Security=True");
        // SqlDataAdapter da = new SqlDataAdapter("select * from emp wher id='" + employeeId + "'", con);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from emp wher id='" + employeeId + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            c.id = dr["id"].ToString();
            c.fname = dr["fname"].ToString();
            c.lname = dr["LNAME"].ToString();
        }

        return c;
    }
    public class Employee
    {
        public string id { get; set; }
        public string fname { get; set; }
        public string lname { get; set; }

    }

error is when I run the application .cs code is firing it executes upto this code

SqlDataReader dr = cmd.ExecuteReader();

after this it is not executing. how to solve this problem

3
  • use try exception, throw.BTW what is the error message.debug your sql Commented Feb 19, 2014 at 9:16
  • 2
    Apart from syntax error, please consider using prepared statements instead of having variables directly in your query. This will attract SQL Injection. Commented Feb 19, 2014 at 9:20
  • In my view the parameter must be CustomerID instead of employeeId. Commented Feb 19, 2014 at 9:21

1 Answer 1

3

Your select query has incorrect syntax:

Try this:

SqlCommand cmd = new SqlCommand("select * from emp where id='" + employeeId + "'", con);
-------------------------------------------------------^

Instead of:

SqlCommand cmd = new SqlCommand("select * from emp wher id='" + employeeId + "'", con);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.