Async table asp.net
I am populating an asp.net table (up to 64 rows) with data from a SQL query and multiple web queries (1 per row). The first 4 columns fill up quickly, however the last 2 take 6 seconds per web request. I would like the last 2 columns to display the loading and refresh gif after the web calls are complete. Since the credentials are passed in the web call, I would like to make server side calls.
Also, I would like to make multiple web requests in parallel. I am researching tasks, however I am not sure how to allow the table to execute when given specific columns.
I am not tied to how I do it, but I am still new to programming and most familiar with C # and asp.net.
Now it looks something like this:
Ajax page
<div>
<form id="ajaxForm" name="ajaxForm" runat="server">
<asp:table id="aspTbl" runat="server" />
</form>
</div>
From C #:
//SQL Connection
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["webConfigConnection"]);
con.Open();
SqlCommand cmd = new SqlCommand("Select name, type, location, IP from tbl", con)
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read()
{
//web requeststring
sURL;
sURL = "http://" + sdr.GetValue(4).ToString() + "WebRequestURL";
WebRequest request;
request = WebRequest.Create(sURL);
request.Method = "GET";
WebResponse response = request.GetResponse();
StreamReader Report = new StreamReader(response.GetResponseStream());
string ReportString = Report.ReadToEnd().ToString();
Response.Close();
//Populate Table
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
TableCell tc4 = new TableCell();
tc1.Text = sdr.GetValue(0).ToString();
tc2.Text = sdr.GetValue(1).ToString();
tc3.Text = sdr.GetValue(2).ToString();
tc4.Text = sdr.GetValue(3).ToString();
tc5.Text = ReportString.SubString(paramaters);
tc6.Text = ReportString.SubString(other paramaters);
TableCell[] tcRow = new TableCell[] { tc1, tc2, tc3, tc4, tc5, tc6 };
tr.Cells.AddRange(tcRow);
asptbl.Rows.Add(tr);
}
The goal is to first get T5 and T6 with the downloadable GIF and update asynchronously with the substring available as soon as.
Edit: 6/10/2015
Thanks for the advice. It works to run web requests concurrently, however it still requires them to complete completely before posting, which I figured out over the weekend, but cannot avoid it on the server. What I will be trying is for the cells to make a jquery ajax call to the web request and update the cells so that columns 1-4 can load instantly and columns 5 and 6 will populate when they appear. Will be updated how it works.
Edit: 6/10/2015 # 2
I updated to fill the cells with blank, and after the ajax call that makes the calls finishes, I scroll through the cells to fill them with javascript. The problem I am facing is that the cell I am trying to fill is calling the same form many times at the same time and the web requests are queued.
source to share
Simply, if necessary,
List<Task> TaskList = new List<Task>();
Task PushRow(Table tbl, string name,string type,string location,string IP ){
Task.Run(()=>{
//Add code for webdownload and table row adding
});
}
Then call this function inside the while loop and store the result in a TaskList of type List <Task>. Use
Task.WaitAll(TaskList.ToArray());
Waiting for the entire task to complete after the cycle.
source to share
I ended up creating a table with blank web query cells. Once the page is loaded, the client makes a web request for each cell and updates the cells. The only thing I couldn't figure out was how to update 2 cells with one web call, but the in-place solution works.
Thanks for watching.
source to share