Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I need to execute a query and fill the value in Dropdownlist box.
I need to do this in JQuery or JSON not in C# end.
Can any one guide me on this please.
Posted
Comments
Peter Leow 25-Nov-13 9:02am    
You still need c# at the back end to query the database.

You can't connect to Database with jQuery.

So, call one C# WebMethod by jQuery and connect to Database.
Then return one object containing all the options and then bind at client side.
 
Share this answer
 
Comments
Member 10318207 26-Nov-13 1:59am    
Dear Tadit Dash, As per your suggestion I have used Webmethod and accessing the same via JQuery but how get the value in array form. The existing below.

[WebMethod()]
public static string fillreqby(string dept)
{
CharbiServerData csd_sta = new CharbiServerData();
DataSet get_data = new DataSet();
SqlParameter[] param = new SqlParameter[27];
param[0] = new SqlParameter("@TYPE", "Ddl_req");
param[3] = new SqlParameter("@DEPARTMENT", dept);
get_data = csd_sta.ExecuteSPWithParameters("charbi..Usp_it_task_entry", param);
if (get_data.Tables.Count > 0)
{
if (get_data.Tables[0].Rows.Count > 0)
{
return "yes";
}
else
{
return "no";
}
}
else
{
return "error";
}
}

Where as in place of Return Yes - I need to store the value in array and bind to dropdownlist box.
Refer the article - Fill a Dropdownlist using jQuery in ASP.NET[^].

You have to serialize the DataTable like below...
JsonConvert.SerializeObject(dataTable);
Member 10318207 26-Nov-13 3:37am    
Thank you, But can you say me where I will get the System.ServiceModel.Web.dll for supporting the JsonConvert. Also can you say me how to retrieve the data in jquery.
For System.ServiceModel.Web.dll - Right Click on Bin Folder -> Add Reference -> .NET Tab -> System.ServiceModel.Web

For the Data retrieval using jQuery, the article provides you the example.

Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...
Member 10318207 26-Nov-13 7:03am    
$.each(jQuery.parseJSON(data.d), function () {
$("#ddl_req_by").append($("<option></option>").val(this['REQ_BY']).html(this['REQ_BY']));
});

I am facing problem here, Error: Uncaught ReferenceError: data is not defined
jQuery (JavaScript) cannot connect to a database such as MySQL, SQL Server, etc. in the traditional sense*.

You must have a script running on the server (in PHP, ASP.NET, etc.) that connects to and queries the database and then returns it to your client-side script over HTTP, usually in JSON, XML, CSV, or another text-based serialization format.



* There have been some ports of SQLite to javascript, which may be worth researching, but more likely are suited to server-side scripting such as in node.js.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900