ASP.NET C# Edit SqlDataSource record without FormView - c#

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.

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

Web Forms 4.5 - Repeater.Items is empty on Postback

I have a Repeater which has it's data set up on Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}
}
When I come to save my data on postback, myRepeater.Items contains zero elements when I know there are several.
protected void btnSave_Click(object sender, EventArgs e)
{
....
// why does this contain zero elements>
foreach (RepeaterItem item in myRepeater.Items)
{
Any suggestions as to what may be the issue?
In this case, binding the repeater even on postback fixed the issue
protected void Page_Load(object sender, EventArgs e)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}

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

Textbox value doesn't get updated

I have a asp.net page with a datalist with a textbox and a button on it, on page load the textbox gets text in it, if I change the text and press the button the text doesn't get updated.
What am I doing wrong?
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table = CategoryAccess.GetProducts();
ProductList.DataSource = table;
ProductList.DataBind();
}
}
protected void btn_Click(object sender, EventArgs e)
{
string Name = textbox.Text;
CategoryAccess.UpdateProducts(Name);
}
}
I had same problem. I found that I put textbox.text = "xxx" in Page_Load() but outside if(!ispostback).
Try to add EnableViewState property in your textBox control and set the value to true.
e.g.
<asp:TextBox ID="textBox1"
EnableViewState="true"
MaxLength="25"
runat="server"/>
or you can do it programatically:
protected void Page_Load(object sender, EventArgs e)
{
textBox1.EnableViewState = true;
}
You need to bing again the new data...
protected void btn_Click(object sender, EventArgs e)
{
string Name = textbox.Text;
// you update with the new parametre
CategoryAccess.UpdateProducts(Name);
// you get the new data
DataTable table = CategoryAccess.GetProducts();
// and show it
ProductList.DataSource = table;
ProductList.DataBind();
}

Categories