Kanwar Singh
Kanwar Singh

Reputation: 908

how to connect with database using jquery in asp.net

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

Upvotes: 2

Views: 5455

Answers (1)

Bhushan
Bhushan

Reputation: 6181

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

Upvotes: 3

Related Questions