this page is the result of redirection from another page and have the company id in the url
<script type="text/javascript">
var url = window.location.search.substring(1);
var CID = url.split("=")[1];//here i take the company id from the link
$.ajax({
type: "POST",
url: "CompanyPage.aspx/ajaxBindData",
contentType: "application/json;charset=utf-8",
data: '{CID: ' + JSON.stringify(CID) + '}',
dataType: "json",
success: function (data) {
alert(data.d);
$("#GridView1").data = append(data.d);
alert("done appending");
$("#GridView1").bind;
alert("done binding");
},
error: function (exception) {
alert(exception.responseText );
}
});
</script>
server side code : //the server side have an class in app_code folder to execute every function in it
public static string ajaxBindData(int CID)
{
/*
SqlDataReader rd = EditingEmployee.FillEmps(CompanyID);
GridView1.DataSource = rd;
GridView1.DataBind();
rd.Close();
*/
DataTable dt = EditingEmployee.GetEmps(CID);
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
string json = JsonConvert.SerializeObject(rows);
return json;
}
editingemployee : //here is the relation between my project and the database
internal static DataTable GetEmps(int CompId)
{
DataTable dt = new DataTable();
try
{
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter sda = new SqlDataAdapter();
conn.Open();
SqlCommand cmd = new SqlCommand("Select Emp_ID,Emp_Name,Company_ID,Emp_Address,Poste_Name, Salary FROM Employee inner join Postes on Postes.PosteID = Employee.Poste_ID Where Company_ID = " + CompId, conn);
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (SqlException ex)
{
return null;
}
}