I am trying to save my GridView data to a database using a foreach loop.
but the program is not entering the loop.
protected void btnAdd_Click(object sender, EventArgs e) {
foreach (GridViewRow row in grdYarnIssue.Rows) {
tbl_YarnIssueTemp item2 = new tbl_YarnIssueTemp();
item2.LotNo = Convert.ToInt32(row.Cells[2].Text.Trim());
item2.Unit = Convert.ToString(row.Cells[3].Text);
item2.IssueQuantity = Convert.ToInt32(row.Cells[4].Text);
item2.Rate = Convert.ToInt32(row.Cells[].Text);
dbcontext.tbl_YarnIssueTemp.AddObject(item2);
dbcontext.SaveChanges();
}
}
Related
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;
}
}
I have a datagridview that is not bound to a table in a database. The datagridview is being populated by the contents of a drop down list and a text box on a button click. I want to prevent the records from being deleted everytime I close the form. Is there a way for the records in the datagridview to be saved without having to create a database table?
Below is my code:
private void btnInsert_Click(object sender, EventArgs e)
{
this.dgInsertedInfo.Rows.Add(ddlVendorID.Text, txtDate.Text);
}
You have many options. Here is a simple solution that uses XML serialization.
Note that it makes a few assumptions:
The data all are strings
The DataGridView already has all the columns
To save the other data types you should create a serializable structure!
private void saveButton_Click(object sender, EventArgs e)
{
List<List<string>> data = new List<List<string>>();
foreach(DataGridViewRow row in dgInsertedInfo.Rows)
{
List<string> rowData = new List<string>();
foreach (DataGridViewCell cell in row.Cells)
rowData.Add(cell.FormattedValue.ToString());
data.Add(rowData);
}
XmlSerializer xs = new XmlSerializer(data.GetType());
using (TextWriter tw = new StreamWriter(yourFileName))
{
xs.Serialize(tw, data);
tw.Close();
}
}
private void loadButton_Click(object sender, EventArgs e)
{
List<List<string>> data = new List<List<string>>();
XmlSerializer xs = new XmlSerializer(data.GetType());
using (TextReader tr = new StreamReader(yourFileName))
data = (List<List<string>>) xs.Deserialize(tr);
foreach (List<string> rowData in data)
dgInsertedInfo.Rows.Add(rowData.ToArray());
}
You can write your OWN class and save it as a setting-property.
Class Settings:
namespace MyNamespace
{
public class Settings
{
private ObservableCollection DataGridItemsProp;
public ObservableCollection DataGridItems
{
get { return DataGridItemsProp; }
set { DataGridItemsProp = value; }
}
}
}
Get and save your setting:
//get settings
var datagrid = Properties.Settings.Default.UserSettings;
//save settings
Properties.Settings.Default.UserSettings= datagrid;
Properties.Settings.Default.Save();
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.
i am new in asp.net i using LINQ with asp.net on button click event my gridview not rebind data and yes gridview is into the updatepanel
'>
'>
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvClientData.Rows)
{
if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
{
string Index = ((Label)gvr.FindControl("lblIndex")).Text;
int GIIndex = Convert.ToInt32(Index);
GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
insertclientinfo.SendToCompany = true;
vjdb.SubmitChanges();
}
}
BindAgencyData();
Response.Redirect(Request.RawUrl);
}
It seems you are trying to modify an object and then saving it back to the DB, but you are doing it wrong.
You are querying the object from a different Data Context, vjdb and you are calling SubmitChanges on linqobject. You should call SubmitChanges on vjdb
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvClientData.Rows)
{
if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
{
string Index = ((Label)gvr.FindControl("lblIndex")).Text;
int GIIndex = Convert.ToInt32(Index);
GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
insertclientinfo.SendToCompany = true;
vjdb.SubmitChanges(); //HERE
}
}
BindAgencyData();
Response.Redirect(Request.RawUrl);
}
Assuming that BindAgencyData is querying database for latest/updated record and then binding the data to the grid.
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();