How to save connection request to database in this scenario - asp.net

I am new to asp.net and know little bit about its working and as I am moving forward I am getting new and more newer doubts.
Here at this point I was working with two RadioLists that are being binded at page load.
Now when a user changes the index of radio button in list 1. Second needs to get updated accordingly depending what value is currently set for selected radio button.
Since the page will be posted back hence I either need to fire a query again to get new data from DataBase for currently selected index or can store 2-3 tables in session in form of Dataset.
What should I do in such scenario. Should I fire an sql query again or retrieve DataSet from Session.
What is the most optimal approach for this. And WHY ?

The data for radio button list 1 (rbl1) should not be retrieved again from the database. It should already be populate from the ViewState. This is an invisible object on your page that keeps track of the contents of your controls between loading them into the browser and returning the form back to the server. If you rebind rbl1 to the data on a postback, you will lose the current selection.
There should be nothing wrong with retrieving the data for the send radiobuttonlist from the database if the results are going to change depending on the selection of the first.
However, if the size of the data is small you might want to cache the results to the application cache, if all users see the same set of data, or the session cache if it is user-dependent.
Then you can use Linq to query the data based on the selection from the first radiobuttonlist.
<asp:RadioButtonList ID="rbl1" runat="server" AutoPostBack = "true"
OnSelectedIndexChanged="rbl1_SelectedIndexChanged">
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//bind your rbl1 here
}
}
protected void rbl1_SelectedIndexChanged(object sender, EventArgs e)
{
//load your second radio button list depending on the selection of the first
}

Related

Should i make a call to DB or save value in viewstate

On asp.net page, I am using tabs and one of the tab has got user control on it.
On the first tab, data is being displayed from table A and the second tab (which has user control on it) is getting data from table B.
On the user control (second Tab), I need to show the column value of table A. It is just one string value.
I am wondering if there is any best way of displaying the value of a table A column without making a call to database?
The way code has been designed, I can’t access the user control’s textbox from the first tab.
I can only think of using view state or session but don’t know if I should use them instead of making call to DB.
I want value to live for the current page's lifecycle.
If you can save it in viewstate then go for it. But there are plenty of storage options in addition to just viewstate:
querystring (good for Ids, not great for strings)
cookie (pretty straight forward)
local storage (HTML 5 only)
cache (you could still appear to make the call but just have the results cached. you then have to deal with cache expiration as well)
session (as you mentioned, this is basically a per-person cache usage, but is not a bad option)
hidden field (basically what viewstate is)
Even with all of those options, the viewstate is going to be a pretty good one, but it just requires that you post back the viewstate every time you need that value.
How about using js to copy the data contents from tab1? Are you loading the usercontrols in tabs using ajax?
If you have a complex form and need to split into smaller chunks I would use a multiview control with as many views as you need to complete your task. If you design each view with its own controls, validation groups and logics .net will do the rest, you won't have to manually deal with states or saving middle steps
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:TextBox ID="txt1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Next" OnClick="NextStep_Click" />
</asp:View>
<asp:View ID="View2" runat="server">
<asp:TextBox ID="txt2" runat="server" />
<asp:Button ID="Button2" runat="server" Text="End" OnClick="EndProcess_Click" />
</asp:View>  
</asp:MultiView>
<asp:TextBox ID="txt3" runat="server" />
In code behind
protected void NextStep_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
txt2.Text = txt1.Text;
}
protected void EndProcess_Click(object sender, EventArgs e)
{
txt3.Text = txt1.Text + " " + txt2.Text;
}
you can go back and forth the times you want and won't have to worry with the values the users entered. Obviously, you have to put buttons to go back and just set the active view you want.

XtraReports Not working in V11.2.8 Version

Currently i am using Devexpress XtraReports V11.2.8. My problem is i am able to populate report in V11.1.6 but i am unable to populate report in V11.2.8 it showing only scroll bar in the reportviewer when i debug it is properly mapping and binding the data. FYI: I need to bind/Initialize reportviewer in button click only. Devexpress people said that i need to initialize reportviewer with the report in page_load it self but we can't do that because we have large amount of data exist and we need to populate report with particular input criteria matched result only. Please advice.
FYI: refer this link for devexpress response on the above issue http://www.devexpress.com/Support/Center/Issues/ViewIssue.aspx?issueid=Q362696
I can't find any solution about version problem without additional information.
but, the 2nd problem you told, i can give a possible solution to that. You can take necessary input you need for the report and then click on the button to preview the report regarding on inputs. It is better to create a stored procedure in your database for this report.
Suppose, you need to show a report for specific 'Cust_Id'. In the 'Form1' put a text field for 'Cust_Id' and a 'button'. On button click event put the following code,
private void simpleButton1_Click(object sender, EventArgs e)
{
Portfolio_XtraReport port = new Portfolio_XtraReport();
string custID = textEdit1.Text.ToString();
port.Portfolio(custID);
port.ShowPreview();
}
now in the 'Portfolio_XtraReport.cs' put the following code,
public Portfolio_XtraReport()
{
Portfolio("N");
}
public void Portfolio(string custId)
{
if (custId != "N")
{
InitializeComponent();
sP_PORTFOLIOTableAdapter.GetData(custId);
}
}
Here, sP_PORTFOLIOTableAdapter is the table adapter for the dataset, I used a stored procedure which takes 'Cust_Id' as input.

ASP.NET and SQL getting values to label

I created a table in sql database which has list of prices and Items Name....
I wrote a small coding to get the values of item names into my dropdownlist....
Now,
If i select an item in the dropdownlist, I need the price to displayed in the textbox or label... How can I do this? help me out!
There are several different ways of doing that.
You could
- load price/product as JS name value collection and do it client side - most efficient way
- populate dropdown with productId (value) and description and handle itemindexchanged event and do everything server side - not recommended for a public facing, busy web apps.
- send ajax call to web service to get price (client side) - that when you have a bit more complicated model.
It's up to you to decide.
Hope that helps.
Set the AutoPostback property of the DropDownBox to True
Then use something like this:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.Value;
}

How to access the bound DataSource item in the ASP.NET ListView's DataBound event?

I know the question has already been posted here but we didn't get to an real solution.
I have bound my ListView to an SqlDataSource and I want to write some text in a control present in the view created in the LayoutTemplate depending on some properties of the rows returned.
Obviously, I'm using the ItemDataBound event to feed my items but this is not the point.
The spontaneous solution was to bind the ListView.DataBound event and access the raw datasource (a DataTable?) and do the required calculations.
I inspected the Items property and, despite it was not empty, the related DataItem property was null.
Do you have any suggestion?
The only work-around I can come to is to execute the calculations in the ItemDataBound event and accumulate the result in some private fields. But it's really ugly to see and makes harder to get some of the required values.
Thanks a lot.
In the ItemDataBound you should be able to access the data source for the Listview through the DataSource property (you might need to cast it to a DataTable):
protected void Listview1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
DataTable sourceData;
sourceData = (DataTable)Listview1.DataSource;
// sourceData is a DataTable, you can run .Compute or whatever you need
}

Asp.Net CreateChildControls() - Re create controls with Postback data - Event

I have a WebPart custom control (Composite) in .Net, which gets created on page load to show a Chart using 'Dundas Charting Controls' (this is created by a user control inside the page). I get the properties for this control from the database.
I have another control, which is a Filter (outside webpart) and based on data of this filter control which the user selects and which I would get on postback after click of button, I have to show the filtered chart results. The problem is CreateChildControls() gets called before the postback data is available (which would be available only after the Page_Load event fires).
I'm unable to get this data in time to pass on the parameters for filtering the Chart Results.
The implementation os like this ...
Webparts
Page > User Control > Webparts > Composite Control/Chart
Filter
Page > User Control > Composite Control [I get this data on Postback]
It sounds like you are running into an event ordering issue. I always try to make my controles relatively dump - so they don't really know how they are being used.
Consider creating a method in your chart control to force an update of its data:
public void UpdateChart(-- arguments as needed --)
then create an event in your composit control (that has your filters) like
public event Eventhandler FiltersChanged;
Assign this to an event hander on parent page:
filterControl.FiltersChanged += new EventHandler(Filter_OnChange)
Then create an event handler that tells your chart control about the change
Filter_OnChange(Object sender, EventArgs e)
{
// get whatever data you need from your filter control
// tell the chart about the new data and have it reload/redraw
myChart.UpdateData( - filter options here -}
}
In doing so, you let the page direct the order of operations and do not rely on the order in which the child controls Load methods are called.
James - Thanks for your answer, but this does not seem to work in my scenario or rather I couldn't make it work, when I tried it. The controls seems to be doing too much and is getting data from every where, it has its own constructor implementation, Load() override etc so a single UpdateChart() function may not have done the trick in this case.
This is what I did, finally.
I fire an Ajax request with Filter Data and set the value in a Session Variable before page does a Postback, this way I get the data at all places/events, and pass on the same as parameter where required. I know it may seem weird way to implement this, but it saved additional Database calls (which in this case are many to create the controls again) even though it comes at the cost of an additional Server HTTP ajax request.
Let me know this implementation can have any negative impact.

Resources