how to bind database field to a drop downs? - c#

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;
}
}

Related

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.

Delete row from objectdatasource before binding to gridview

I have some data in ObjectDataSource, before binding the data to the GridView, I want to remove some rows from the DataSource.
This is what I am trying:
protected void gvExitInterview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
User employee = (User) e.Row.DataItem;
if(//some condition here)
{
//do nothing
}
else
{
//delete the row
this.gv.DeleteRow(e.Row.RowIndex);
return;
}
}
}
These are my deletion methods:
protected void gvExitInterview_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void gvExitInterview_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
gv.DataBind();
}
This is my grid binding:
private void BuildGrid(DateTime from, DateTime to)
{
this.objDS.TypeName = "EmployeeManagement";
this.objDS.SelectMethod = "GetEmployees";
this.objDS.SelectCountMethod = "GetEmployeesCount";
this.objDS.SelectParameters.Clear();
this.objDS.SelectParameters.Add("from", from.ToString());
this.objDS.SelectParameters.Add("to", to.ToString());
this.objDS.SelectParameters.Add("csvEntities", csv);
this.objDS.SelectParameters.Add("sortExpression", ViewState["SortColumn"].ToString());
this.gv.DataSource = objDS;
this.gv.DataBind();
}
This is not working, it does not filter or delete any data from the grid. Any idea how to do explicit deletion?
I think you need to write another SelectMethod in the DataObjectClass which get a parameter which you use pass to filter condition. So that just returning with the rows required to display.

Drop Down List make list item requery entitydatasource

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();

Retrieving values of dynamically created controls on Post back in Asp.Net

I need to dynamically add CheckBoxList on the SelectedIndexChanged event of DropDownList. I have achieved this but I cannot retain its value on postback.
Here’s what I have done so far:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
loadTracks();//Needs to generated dynamically
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadDegrees();
}
loadTracks();
}
public void loadTracks()
{
try
{
ConfigurationDB objConfig = new ConfigurationDB();
DataSet ds = objConfig.GetTracksByDegreeID(
Convert.ToInt32(ddlDegree.SelectedValue.ToString()));
CheckBoxList CbxList = new CheckBoxList();
CbxList.ID = "Cbx";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CbxList.Items.Add(new ListItem(ds.Tables[0].Rows[i]["Track_Name"]
.ToString(), ds.Tables[0].Rows[i]["ID"].ToString()));
}
ph.Controls.Add(CbxList);
ViewState["tracks"] = true;
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
//For testing, I added a button and on its click I have added this code
protected void btnDetails_Click(object sender, EventArgs e)
{
CheckBoxList Cbx = (CheckBoxList)ph.FindControl("chk");
foreach (ListItem ex in Cbx.Items)
{
if (ex.Selected)
{
Response.Write(String.Format("You selected: <i>{0}</i> <br>", ex.Value));
}
}
}
Might be a typo:
CbxList.ID = "Cbx";
v.s.
CheckBoxList Cbx = (CheckBoxList)ph.FindControl("chk");
You can try it without changing the code and use pre PreRender
just run you loadTracks()

Categories