Interfacing with server-side code

Top  Previous  Next

If you need to invoke server-side code to perform a server-side function such sending an e-mail or to interface directly with the database you can do so with AJAX calls made using the client-side DbNetLink.Util.webMethod function. This function allows you to invoke a server-side method labeled with the WebMethod attribute directly.

 

In the following example the option to create a custom CSV file is added to an application by adding a button which invokes the client-side createCSV function.

Client-side Code

	////////////////////////////////////////////////////////////
	function createCSV()
	////////////////////////////////////////////////////////////
	{
		var data = {"Filter" : DbNetLink.components["dbnetgrid1"].fixedFilterSql}
		DbNetLink.Util.webMethod("CreateCSV", window.createCSVCallback, data);
	}

	////////////////////////////////////////////////////////////
	function createCSVCallback(response)
	////////////////////////////////////////////////////////////
	{
		jQuery(".my-update-button").attr("disabled","");
		jQuery(".csv-text").val(response);
	}

 

The call to the webMethod function has 3 arguments. The first argument is the name of the server-side Web Method. The second is the client-side method that will be called when the server-side code completes. The 3 is a client-side object that contains any arguments that are passed to the server-side web method. The property name is the name of the argument and the property value is value passed to the server-side method.

Server-side Code

[WebMethod]
///////////////////////////////////////////////////////////////
public static string CreateCSV(string Filter)
///////////////////////////////////////////////////////////////
{
	QueryCommandConfig Query = new QueryCommandConfig();
	Query.Sql = "select * from products where " + Filter;
	DbNetData Db = new DbNetData("SamplesDatabase");
	Db.Open();
	Db.ExecuteQuery(Query);

	StringBuilder CSV = new StringBuilder();

	while (Db.Reader.Read()) {

		List Values = new List();
		Values.Add( Db.ReaderString("ProductName") );
		Values.Add( Db.ReaderString("QuantityPerUnit") );
		Values.Add( Db.ReaderString("UnitPrice") );
		Values.Add( Db.ReaderString("UnitsInStock") );
		Values.Add( Db.ReaderString("UnitsOnOrder") );
		Values.Add( Db.ReaderString("ReorderLevel") );

		CSV.Append( String.Join(",",Values.ToArray()));
		CSV.Append( Environment.NewLine );
	}

	Db.Close();

	return CSV.ToString();

}

Run Sample