Linq to Entity insert data in db - c#

How can I insert data added in GridView after clicking Save buton on BidingNavigator using Linq to Entity.
I am trying to add data to database but some problem occurs. I'm showing data using this code:
...
private void PostojeciPRojekti_Load(object sender, EventArgs e)
{
dbcontext = new logicrms2Entities1();
var projekti = from c in dbcontext.projekti
select c;
projektiBindingSource.DataSource = projekti.ToList();
projektiGridControl.Refresh();
}
...
EDIT
I have try to save data using:
private void projektiBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
dbcontext.SaveChanges();
gridView1.RefreshData();
}

First maintain a list of newly added objects in your form:
private List<projekti> addedList = new List<projekti>();
Then, handle RowsAdded event on your DataGridView:
dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
Next, add any added object to the list in your RowsAdded event handler :
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
addedList.Add(dataGridView1.Rows[e.RowIndex].DataBoundItem as projekti);
}
Finally, in your navigator's save button click event handler method you can store the objects:
private void saveButton_Click(object sender, EventArgs e)
{
var dbcontext = new logicrms2Entities1();
addedList.ForEach(p=> dbcontext.projeckit.Add(p));
dbcontext.SaveChanges()
}

Related

How can I call one function from other function with RoutedEventArgs in C#?

I'm trying to populate a datagrid calling a function with RoutedEventArgs event. So far it works, but I was expecting to reuse the function on a TabControl SelectionChanged, but when I reference the previous function, I get a NullReferenceException. I think the event was handled before on TabControl event, so I'm stuck on this puzzle, since how could I reap the previous event. Here are the functions:
private void BtnMostrar(object sender, RoutedEventArgs e)
{
ObservableCollection<EventoRubrica> cltEvtRubrica =
new ObservableCollection<EventoRubrica>();
using (var db = new EventoRubricaContext())
{
var evts = db.EventoRubricas;
foreach(var evt in evts)
{
cltEvtRubrica.Add(evt);
}
}
evtDataGrid.ItemsSource = cltEvtRubrica;
}
private void tbCtrl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
BtnMostrar(sender, e);
}

Reset sorting at DataGridView event

I need to reset my sorting everytime I choose different column how can I achieve this in the code
private void AdvancedDataGridView_SortStringChanged(object sender, EventArgs e)
{
this.bindingDBSource.Sort = this.advancedDataGridView.SortString;
}

how to maintain state for a table in asp.net

i am trying to upload a csv file data to DB.
the process is like :
i am extracting csv file data to DataTable.
validating DataTable fields .
if all good , loading them into DB on button click.
i am generating invTable at below validation method by passing saved csv file path on validate button click.
protected void ValidateData_Click(object sender, EventArgs e)
{
ValidateCsv();
}
private void ValidateCsv(string fileContent)
{
DataTable getCSVData;
getCSVData = GetData(fileContent);
invTable= Validate(getCSVData);
}
if am loading it on BulkUpload_Click, invTable is null.
ideally it should have data , as assinged in ValidateCsv method.
protected void BulkUpload_Click(object sender, EventArgs e)
{
MatchedRecords(invTable);
}
any idea how to maintain invTable data across postback?
Change your ValidateCsv to a function.
private DataTable ValidateCsv(string fileContent)
{
DataTable getCSVData;
getCSVData = GetData(fileContent);
invTable = Validate(getCSVData);
return invTable;
}
Then on your MatchedRecords, modify it to accept a DataTable parameter.
private void MatchedRecords(DataTable invTable) {
}
Then on your BulkUpload_Click event, reference to the ValidateCsv, now a function that returns the value of your DataTable to the MatchedRecords void.
protected void BulkUpload_Click(object sender, EventArgs e)
{
MatchedRecords(ValidateCsv('enter how you get the fileContent here...'));
}
Or
protected void BulkUpload_Click(object sender, EventArgs e)
{
DataTable valueTable = ValidateCsv('enter how you get the fileContent here...');
MatchedRecords(valueTable);
}

Check if a value already exist in db and bound dgv before adding/saving

I have a bound datagridview and I want to check before inserting a username if that username already exists or not in my database. I insert values to dgv and database through a textBox. Thanks in advance..
private void user1BindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.user1BindingSource2.EndEdit();
this.tableAdapterManager2.UpdateAll(this.databasDataSet);
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'databasDataSet.user1' table. You can move, or remove it, as needed.
this.user1TableAdapter2.Fill(this.databasDataSet.user1);
user1DataGridView.Columns[0].Visible = false;
passwordTextBox.PasswordChar ='*';
}
private void add_new_button_Click(object sender, EventArgs e)
{
this.user1BindingSource2.AddNew();
}
private void save_button_Click(object sender, EventArgs e)
{
this.Validate();
this.user1BindingSource2.EndEdit();
this.tableAdapterManager2.UpdateAll(this.databasDataSet);
}
private void delete_button_Click(object sender, EventArgs e)
{
this.user1BindingSource2.RemoveCurrent();
}
To validate anything on the server-side (the existence of the username in your case) there is CustomValidator control. Add it to your page and attach it to your username TextBox. Then place your validation code to ServerValidate event handler.

Problem displaying data in gridview with object data source (L2S)

I have a gridview using object datasource for data binding. Everything is working fine except, When i add some new records to data it is not displaying immediately, it requires a refresh. I am using L2S Business Object with Object Data Source. Same thing in update and delete events.
I think you miss EditIndex property, change it on every event, like :
protected void HlnkbInsert_Click(object sender, EventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
...
gv.EditIndex = e.NewSelectedIndex;
DataBindGV();
}
Are you re-binding your GridView after making the changes to your data?

Categories