Drop Down List make list item requery entitydatasource - c#

My first post here and Iam an absolute beginner. Searched the web for hours. I feel that I might have approached my problem the wrong way, but here goes.
I have a Datasource that displays Loans (assets) in a Gridview.
I would like to have a ddl to filter loans. Like: `If returneddate !=null the items in grid will be free for a new loan.
Selecting ex. "Available assets" in ddl runs a where query on entitydatasource and retrieves the filtered data into grid.
My code: A bit of a mess, several queries that hopefully do the same. I prefer the first one LinqtoEntities
namespace Logsys.Pages
{
public partial class OversiktLån : Page
{
private LogsysEntities context = new LogsysEntities();
protected void Page_Load(object sender, EventArgs e)
{
}
protected EntityDataSource LaanLedig(object sender, EventArgs e)
{
var ledigQuery = from laan in context.Laans
where laan.Returnertdato != null
select laan;
foreach (var laan in ledigQuery)
{
}
}
protected void DDLlaan_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void LaanEntityDataSource_QueryCreated(object sender, QueryCreatedEventArgs e)
{
var laanQuery1 = e.Query.OfType<Laan>();
e.Query = from c in laanQuery1
where (c.Returnertdato != null)
select c;
How to get result of query "into" datasource and make ddl items trigger queries?
KK

protected void ddlLaan_SelectedIndexChanged(object sender, EventArgs e)
{
Int32 ddlvalue = Convert.ToInt32(ddlLaan.SelectedValue);
if (ddlvalue == 1)
{
CLogsysEntities = new LogsysEntities();
var ledig =
from laan in CLogsysEntities.Laans
where laan.Returnertdato != null
select laan;
LaanGridView.DataSourceID = null;
LaanGridView.DataSource = ledig.ToList();
LaanGridView.DataBind();

Related

how to bind database field to a drop downs?

i'm requesting data from database through a D A L file. I want to data bind and show a drop down menu's single option selected against that of the database. How should i do it?
here's my code:
Pages pg = new Pages();
public static string pgId;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Bind_ddlParentPage();
Bind_ddlPageModel();
Bind_ddlArticleModel();
if (!IsPostBack)
{
pgId = Request.QueryString["pageId"];
if (pgId != null)
{
GetData();
}
}
}
}
public void GetData()
{
ddlParentPage.SelectedValue = pg.ParentPage;
//Bind_ddlParentPage();---dropdownlist which is causing problem.
//I want to set this data:: pg.ParentPage to dropdownlist in another
page
ddlPageModel.SelectedValue = pg.PageModel;
//Bind_ddlPageModel();
//All the three drop downs have same table for the source,
'Pages' table and this page is the same page for adding new entry to
Pages table.
ddlArticleModel.SelectedValue = pg.ArticleModel;
//Bind_ddlArticleModel();
}
First, you have a duplication in your conditional statement:
if(!IsPostBack) {
...
if(!IsPostBack) {
...
}
}
Second, you need to bind the data to the dropdown, then set the selected value. Refer: this post
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
}
}

Temporary Storage Datalist c#

So I have this problem with storing temporary data, basically the effect I'm after is something like this Link
My problem is when I do it on List or Binding list, it won't save the old rows and just change it to the new ones.
Here is the code I got
BindingList<Genrer> Film_Genrer = new BindingList<Genrer>();
Genrer genrer = new Genrer();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList_Genrer.DataSource = Db.SelectAllFrom("Genrer");
DropDownList_Genrer.DataTextField = "genrer_navn";
DropDownList_Genrer.DataValueField = "genrer_id";
DropDownList_Genrer.DataBind();
}
}
protected void Button_AddGenrer_Click(object sender, EventArgs e)
{
Genrer genrer = new Genrer();
genrer.Navn = DropDownList_Genrer.SelectedValue;
Film_Genrer.Add(genrer);
GridView1.DataSource = Film_Genrer;
GridView1.DataBind();
}
In the Button_AddGenrer_Click method, your Film_Genrer is initially empty and you just add one item to that and set it as DataSource. What you need to do is first add all your items to Film_Genrer, then add the new item and then set the DataSource.

ASP.NET C# Edit SqlDataSource record without FormView

How can Edit SqlDataSource record without FormView?
I knows getting data into TextBox, But can't save changes.
protected void Page_Load(object sender, EventArgs e)
{
DataView dvSql = (DataView)EmployData.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drvSql in dvSql)
{
E_ID.Text = drvSql["ID"].ToString();
E_LName.Text = drvSql["LName"].ToString();
...
}
}
protected void SaveChanges_Click(object sender, EventArgs e)
{
EmployData.UpdateParameters.Add("ID", E_ID);
EmployData.UpdateParameters.Add("LName", E_LName);
....
EmployData.Update();
}
Thank's.
So simple...
Add on Load_Page before read code:
if (Page.IsPostBack)
{ }
else
{
DataView dvSql = (DataView)EmployData.Select(DataSourceSelectArguments.Empty);
....
....
}
Good Day.

Unable to get search result in the list. c# WPF

private void SearchButton_Click(object sender, RoutedEventArgs e)
{
EmployeeDataDataContext con = new EmployeeDataDataContext();
List<Employee> employees = (from s in con.Employees
where s.Name.ToLower() == SearchBox.Text.ToLower()
select s).ToList();
EmployeeGrid.ItemsSource = employees;
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
Employee selected = EmployeeGrid.SelectedItem as Employee;
if (selected == null)
MessageBox.Show("You must select the employee.");
else
{
EditEmployee employee = new EditEmployee(selected);
employee.ShowDialog();
}
}
could not getting anything in the list wheni press the search button .
It sounds like you want to do an partial match. You can use string.Contains for that
where s.Name.ToLower().Contains(SearchBox.Text.ToLower())
That will allow your text to match anywhere within the name column.

DataGridView Does not refresh after editing in c#

I have a data grid view whose data source gets assigned a list of items after the following function on load:
public void refreshGrid(object sender, FormClosingEventArgs e)
{
dgvItems.SuspendLayout();
itemBindingSource.SuspendBinding();
List<Item> items = db.Items.ToList(); // db is MyContext db = new MyContext();
itemBindingSource.DataSource = items;
dgvItems.DataSource = null;
dgvItems.DataSource = itemBindingSource;
itemBindingSource.ResumeBinding();
dgvItems.ResumeLayout();
}
private void AllItemsForm_Load(object sender, EventArgs e)
{
refreshGrid();
}
and there is a edit button which does the following on click:
private void btnEditItem_Click(object sender, EventArgs e)
{
Item item = (Item)dgvItems.SelectedRows[0].DataBoundItem;
var editForm = new EditItemForm(item);
editForm.FormClosing += new FormClosingEventHandler(refreshGrid);
editForm.Show();
}
i.e. opens an edit form and assigns refreshGrid() to its closing event.
On that Edit Form I have this Save button which does this:
private void btnSave_Click(object sender, EventArgs e)
{
Item itemEdited = db.Items.Where(i => i.itemId == itemEditing.itemId).Single();
itemEdited.categoryId = (int)cbxCategory.SelectedValue;
itemEdited.description = tbxDescription.Text;
itemEdited.price = (Double)nudPrice.Value;
db.Entry(itemEdited).State = EntityState.Modified;
db.SaveChanges();
this.Close();
}
the item edit is working, but is apparent only after closing and reopening the edit form, i.e. that refreshGrid() method which was assigned to its closing event is not working!
How can I fix this?
I found my own mistake. The mistake was using two different instances of Context class.
The solution was to add:
SomsaContext database = new SomsaContext(); // i.e. new instance of Context class
right before the refresh takes place.

Categories