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
Suraj Shrestha
1,8181 gold badge26 silver badges51 bronze badges
asked Feb 19, 2014 at 9:14
Kanwar Singh
90812 silver badges21 bronze badges
-
use try exception, throw.BTW what is the error message.debug your sqlKumarHarsh– KumarHarsh2014年02月19日 09:16:35 +00:00Commented Feb 19, 2014 at 9:16
-
2Apart from syntax error, please consider using prepared statements instead of having variables directly in your query. This will attract SQL Injection.Java_User– Java_User2014年02月19日 09:20:05 +00:00Commented Feb 19, 2014 at 9:20
-
In my view the parameter must be CustomerID instead of employeeId.Suraj Shrestha– Suraj Shrestha2014年02月19日 09:21:34 +00:00Commented Feb 19, 2014 at 9:21
1 Answer 1
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);
answered Feb 19, 2014 at 9:17
Bhushan
6,19113 gold badges61 silver badges93 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default