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);
}
Related
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.
I have a Gridview and I want to set Pagination. So far I have:
<asp:GridView ID="grdActivity"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
PageSize="30"
OnPageIndexChanging="gridView_PageIndexChanging">
C# code on the back is:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataBind();
}
I get no errors but when I press any other page (2 and on), nothing shows, just a blank page and the GridView disappears. Am I missing something? I see hundreds of pages showing this exact code to do this.
You will need to re-assign your datasource property. After the postback the grActivity.DataSource is null, and calling DataBind on it will clear the grid.
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//re-assign your DataSource
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
//Set the new page index
grdActivity.PageIndex = e.NewPageIndex;
//apply your changes
grdActivity.DataBind();
}
Some notes:
you should consider re-factoring the code that brings you the data into a single method called on Page_Load and inside the gridview_PageIndexChanging (see #ekad's answer)
if you have a lot of data you should consider paging the data directly on the database. Right now if your table has 500 rows all will be loaded into the dataset even if just a small part of it (the page size) will be displayed in the grid
Hi you please keep the code in a separate method and call it in page load event and page index changed event, something like
protected void FillGrid()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
and in the page index changing event
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
FillGrid();
}
and in page load in !post back event simply call the method, like
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillGrid();
}
}
You have to set the data source of grdActivity before calling grdActivity.DataBind() in gridView_PageIndexChanging method. I would also suggest creating a separate method to avoid repeating the same code in Page_Load and gridView_PageIndexChanging
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}
}
private DataTable GetDataSource()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
return myDataSet.Tables["tblActivity"];
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}
Few days ago start programing in SilverLight and have a bit of a problem with saving data in local a variable from database by using wcf&linq. The connection and data result are working.
public void load(){
ServiceReference.ServiceClient serv=new ServiceReference.ServiceClient();
serv.GetAssetListCompleted+=new EventHandler<ServiceReference1.GetAssetListCompletedArgs>(serv_GetAssetListCompleted);
serv.GetAssetListAsync();
}
void serv_GetAssetList(object sender, ServiceReference.GetAssetListCompletedEventArgs e)
{
datagrid1.ItemsSource=e.Result;
}
This code is working and my datagrid1 is filling up with element.(AutoGenerateColumns="True") in Xaml-code. But I want to save this datagrid into a list or something else. Because I want to use these information like local variable.
Tried so many code like:
void serv_GetAssetList(object sender, ServiceReference.GetAssetListCompletedEventArgs e)
{
var result_list=e.Result.toList();
}
Or
void serv_GetAssetList(object sender, ServiceReference.GetAssetListCompletedEventArgs e)
{
string[] temp=new string[e.Result.Count];
for (int i=0;i<e.Result.Count;i++)
temp[i]=e.Result[i].Id;
}
But none work. All of them show an empty variable.
Try this:
void serv_GetAssetList(object sender, ServiceReference.GetAssetListCompletedEventArgs e)
{
MessageBox.show(e.result.count);
}
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()
}
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?