ASPNet Dropdown List from 0 to quantity available - mysql

I'm creating a webform for a Class assignment that essentially allows you to select a quantity from 0 to the amount available for each product. We're using AdventureWorks 2014 as our datasource.
However it only displays the maximum quantity and not from 0 to the max quantity.
I'm just stuck on what to add so it can display 0 to max quantity. Thanks.
I don't have anything in regards to the code behind, it's just the basic:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOrder_Click(object sender, EventArgs e)
{
}
protected void ddlQuantity_SelectedIndexChanged(object sender, EventArgs e)
{
}
}

You can perform binding by handling SelectedIndexChanged event from ddlProductName to fill ddlQuantity items:
ASPX
<asp:DropDownList ID="ddlProductName" runat="server" AutoPostBack="True" DataSourceID="ddlProductNameitems" DataTextField="Name" DataValueField="Name"
OnSelectedIndexChanged="ddlProductName_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlQuantity" runat="server" AutoPostBack="True" ...></asp:DropDownList>
Then use a List collection to store all numbers from 0 to maximum value set by SUM query then bind that List to DropDownList in SelectedIndexChanged event given above:
ASPX.cs (code-behind)
protected void ddlProductName_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> quantities = new List<string>();
int maxQuantity = 0;
// retrieve SUM result from SqlDataSource
// if 'DataSourceSelectArguments.Empty' doesn't work, try other 'DataSourceSelectArguments' options
DataView view = QuantityChoices.Select(DataSourceSelectArguments.Empty) as DataView;
// set maximum quantity from SUM query result
if (view != null)
{
maxQuantity = int.Parse(view.Table.Rows[0]["TotalInventory"].ToString());
}
else
{
// assign maxQuantity from SqlConnection here
}
// add every quantity amount to the list...
for (int i = 0; i <= maxQuantity; i++)
{
quantities.Add(i.ToString());
}
// ... then sort from least value...
quantities.Sort();
// ... and bind the list here!
ddlQuantity.DataSource = quantities;
ddlQuantity.DataBind();
}
NB: System.Data namespace should be added to use DataView component. Note that it may necessary to remove DataSourceID, DataTextField & DataValueField from ddlQuantity to bind corresponding DropDownList with generated list of quantities.

Related

I am unable to retrieve column value in a Telerik radgrid.

I am trying to select a column value in a selected row but I am unable to select the row. It seems that after clicking on the row to be selected, it does not go into the if statement. If I change the if statement to (dataItem.Selected = true) with only one "=", it goes in but returns the invoice id for all of the rows. Any advice on how to resolve this issue?
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
var a ="";
foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
{
if (dataItem.Selected == true)
{
a = dataItem.GetDataKeyValue("InvoiceId").ToString();
Response.Write(a);
}
}
}
Do you allow multiple selections or only just single selection in your RadGrid?
To me, the foreach loop code block doesn't seem correct if you do single row selection.
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
// get selected row
GridDataItem item =(GridDataItem)RadGrid1.SelectedItems[0];
}
Also, another point is you should set EnablePostBackOnRowClick property to true so that RadGrid's SelectedIndexChanged event will be fired properly on the server side.
<ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
</ClientSettings>
But if you wish to fire row select command from client side, then you should add a row click event with JS.
function RowClick(sender, eventArgs) {
sender.get_masterTableView().fireCommand("Select", eventArgs.get_itemIndexHierarchical());
}
Then associate that JS function to <ClientEvents OnRowClick="RowClick" /> in your aspx.
You can use RadGrid1.SelectedItems[0] To get your selected item.
protected void RadGrid1_ItemChanged(object sender, EventArgs e)
{
var myDataItem = RadGrid1.SelectedItems[0] as GridDataItem;
if (myDataItem != null)
{
var name = myDataItem ["InvoiceId"].Text;
}
}
And Woodykiddy is right. Check your postback and your allow row selecting.
And if you re using ajax dont forget the rad Ajax manager and panel.

Loading data into a textbox and then changing it

i have made a textbox that gets it's text from a variable linked to a database.
ee from class employee and s from class general both work, and the data inside ee is correct.
when the page loads, the textbox does show the data inside ee.Field but when i change it and click save it doesnt change and doesnt save the new data in my database , i know for sure the the functions.fieldChange() works and that for some reason it doesnt get into the if(field.text!=ee.Field) (i have checked it using a simple label text change).
here is my html:
<asp:TextBox ID="field" runat="server"></asp:TextBox><br />
<asp:Button ID="Save" runat="server" Text="Save" OnClick="saveChanges" />
my asp.net:
string User;
Genral s = new Genral ();
public Employee ee;
protected void Page_Load(object sender, EventArgs e)
{
User = Session["User"].ToString();
ee = s.getEmployee(User);
this.field.Text = ee.Field;
}
protected void saveChanges(object sender, EventArgs e)
{
if (field.Text != ee.Field)
{
s.fieldChange(User, field.Text);
}
}
What doesnt it work? Thanks for the help
You need to check ispostback property in page load, when you hit save button it first called postback so it replace the value with the old one and your newly inserted data lost.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
User = Session["User"].ToString();
ee = s.getEmployee(User);
this.field.Text = ee.Field;
}
}

How do I refresh/update a LongListSelector after removing a MenuItem (on Windows Phone 8)?

I am trying to delete a MenuItem from a LongListSelector in my Windows Phone 8 app. The MenuItems play various sounds when clicked and I want the user to be able to delete them.
There are two panels on the app. The second panel records a new sound and puts the recording on the LongListSelector as a new MenuItem.
Problem: After I do the delete the display looks exactly the same and the sound still plays! However, if I record a new sound (switching to the new recording panel) then the deletion works with the deleted MenuItem gone.
How do I force the update/refresh of the LongListSelector to unload/delete the MenuItem and associated sound data resident on the GUI?
The following code is called from from the MenuItem's click event. The LongListSelector is named 'CustomSounds':
private void DeleteSoundClick(object sender, RoutedEventArgs e)
{
var menuItem = sender as MenuItem;
if (menuItem == null) return;
var soundData = menuItem.DataContext as SoundData;
if (soundData == null) return;
if (soundData.FilePath.Contains(CustomSounds.Name))
{
CustomSounds.ItemsSource.Remove(soundData);
}
this.LayoutRoot.UpdateLayout();
}
Inverse your thing. Set item source with new list.
private void DeleteSoundClick(object sender, RoutedEventArgs e)
{
var menuItem = sender as MenuItem;
if (menuItem == null) return;
var soundData = menuItem.DataContext as SoundData;
if (soundData == null) return;
if (soundData.FilePath.Contains(CustomSounds.Name))
{
MyNewList.remove(soundData);
CustomSounds.ItemsSource = myNewList;
}
this.LayoutRoot.UpdateLayout();
}
Create local variable and set your itemsource in constructor.
Other thing :
create an updated list :
private ObservableCollection<Sound> _myNewList;
public ObservableCollection<Sound> MynewList{
get
{
return _myNewList;
}
set
{
_myNewList= value;
RaisePropertyChanged(MynewList);
}
Bind this in your listbox :
<listbox itemSource="{Binding MyNewList" SelectedItem="{Binding SelectedSound,mode=twoway}>
Create selectedSound:
private Sound _selectedSound;
public Sound SelectedSound{
get
{
return _selectedSound;
}
set
{
_selectedSound= value;
RaisePropertyChanged(SelectedSound);
}
Delete item :
private void DeleteSoundClick(object sender, RoutedEventArgs e)
{
if(SelectedSound != null){
Mynewlist.remove(SelectedSound);
}
}
:D
ObservableCollection is nothing more than a collection with notification that when something is changed in the collection, it lets the UI know. It's just a ItemSource, so your LongListSelector is populated by the OC...
I had the same issue, however, I used simple List.
Solution was easy - just created new object of the List
var newList = new List<TheModel>();
newList.AddRange(originalList);
TheLongListSelector.ItemsSource = newList;
TheLongListSelector.UpdateLayout();
originalList was list with removed item.
It's not the most efficient way, but it works. I think that's no problem for small data.

Microsoft Reporting with Collection as DataSource

I'm working with MS Reporting Services. The underlying datasource is
IEnumerable<MyObject>, I'm not using DataSets.
Every MyObject has properties and some other IEnumerable collections.
In the report I want to display all the properties from MyObject and
the collections lists too.
I didn't know how to display this inner collections, so I've made a SubReport to which I passed the MyObject.Id so that the SubReport could retrieve the object by himself and Build a the DataSource for these inner collections.
I do this in this event.
myReportViewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
int id;
if (e.Parameters.Count > 0 && int.TryParse(e.Parameters[0].Values[0], out id))
{
MyObject current = myObjects.Where(x => x.MyObject.Id == id).FirstOrDefault();
InnerListBindingSource.DataSource = current.InnerCollection;
e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource(
"MyInnerCollectionDataSource", InnerListBindingSource));
}
}
But there is always "The SubReport could not be shown" in my Master Report.
(Master report - subreport are correctly binded)
Any Idea why? Or how to resolve this in a more elegant way ?
Thank you
OK.
So I went to this solution and it's working:
private IEnumerable<MyObject> myObjects;
public ReportViewerForm(IEnumerable<MyObject> myObjects)
{
InitializeComponent();
this.myObjects = myObjects;
this.WindowState = FormWindowState.Maximized;
ReportViewer reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.ReportEmbeddedResource = #"SomePath." + "Report1.rdlc";
/*reportViewer.LocalReport.ReportPath = #"SomePath\Report1.rdlc"; */
reportViewer.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
reportViewer.LocalReport.DataSources.Add(
new ReportDataSource("MyDataSource", myObjects));
reportViewer.LocalReport.SetParameters(new List<ReportParameter>
{
new ReportParameter("param1", ..WhatEver..),
...
});
reportViewer.Dock = DockStyle.Fill;
this.panel1.Controls.Add(reportViewer);
reportViewer.RefreshReport();
}
void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
/* For example ID parsing.. when you have it defined in .rdlc file.. */
int id;
if (e.Parameters.Count > 0 && int.TryParse(e.Parameters[0].Values[0], out id))
{
MyObject current = myObjects.Where(x => x.MyObject.Id == id).FirstOrDefault();
e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource(
"InnerListDataSource", current.InnerList));
}
}
If I understand you correctly, you have a structure that resembles a table. So why don't you take a DataTable? ReportingServices offers easy access to those.
Or did I get you wrong there?

LinqDataSource query help

I know LINQ doesn't have the SQL IN clause but rather uses "contains". Can I use this on a LinqDataSource? I want to write a simple query that is equivelent to this:
SELECT * FROM tableA
WHERE tableA.requestType NOT IN (5,6,7,8) AND
tableA.someBitField IS NULL.
Is this possible using the LinqDataSource out of the box?
Thanks for any pointers.
Cheers,
~ck in San Diego
Yes, quite possible. Just handle the Selecting event on the datasource. The LinqDataSoruce class page on MSDN contains a great example already. Modifying that:
public partial class Default3 : System.Web.UI.Page
{
int[] validRequestTypes = { 5, 6, 7, 8 };
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
using(var dc = new MyDataContext())
{
var qry = from item in dc.tableAs
where validRequestTypes.contains(item.requestType)
&& item.someBitField == null
select item;
e.Result = qry;
}
}
}