Newly inserted record does not appear in my GUI.  
Author Message
preps





PostPosted: .NET Framework Data Access and Storage, Newly inserted record does not appear in my GUI. Top

Hi
I am a newbie in C# and GUI development. I have a Windows.System.Form that has three panels.
First panel is a collection of two buttons viz. "Insert New Record" and "Edit Record" buttons.
Insert button takes you to second panel where it has comboboxes and textbox to enter the data that is then inserted into the database that is in /bin/Debug/databasename.mdb
When I click on my second button which is 'Edit Record' it takes me to my third panel which displays' a list of records in the database. But for some reason, the newly inserted record(in the previous panel) is not being displayed.
I would appreciate if anyone could point me towards a possible solution.
Thanks in advance!





.NET Development32  
 
 
ahmedilyas





PostPosted: .NET Framework Data Access and Storage, Newly inserted record does not appear in my GUI. Top

it would help if you posted some code! how are you saving the records how is it editing

 
 
preps





PostPosted: .NET Framework Data Access and Storage, Newly inserted record does not appear in my GUI. Top

1>Below is the code for Insert operation that stores the new record in DB.

private void Insertbutton_Click(object sender, EventArgs e)
{
//string conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\preps\\My Documents\\Visual Studio 2005\\Projects\\GTIApplication\\GTIApplication\\GTI_DB.mdb;User Id=admin; Password=";
//string query_string = "SELECT COUNT(*) FROM Flat_Data";
string query_string = "SELECT Max([ID]) AS Expr1 FROM Flat_Data;";
OleDbConnection oleconnection = new OleDbConnection(conn_str);

short id1 = Convert.ToInt16(Execute_Query(query_string, oleconnection));
short id2 = Convert.ToInt16(this.MainCatcomboBox.SelectedValue);
short id3 = Convert.ToInt16(this.Level1comboBox.SelectedValue);
short id4 = Convert.ToInt16(this.Level2comboBox.SelectedValue);
short id5 = Convert.ToInt16(this.Level3comboBox.SelectedValue);
short sem_id = Convert.ToInt16(this.Sem_YrcomboBox.SelectedValue);
string d = this.richTextBox1.Text;
//Increment the primary key value.The query returns largest primary value existing in the DB.
id1++;
this.flat_DataTableAdapter.Insert(id1, id2, id3, id4, id5, sem_id, d);
try
{
this.Validate();
this.flatDataBindingSource.EndEdit();
this.flat_DataTableAdapter.Update(this.gTI_DBDataSet.Flat_Data);
//.Update(this.gTI_DBDataSet.Flat_Data);
MessageBox.Show("Insert to DB successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Insert to DB failed");
}
}

object Execute_Query(string query_string,OleDbConnection oleconnection)
{
object returnvalue;
OleDbCommand command = new OleDbCommand(query_string, oleconnection);

try
{
command.Connection.Open();
returnvalue = command.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (oleconnection.State == ConnectionState.Open)
oleconnection.Close();
}

return (returnvalue);
}

2>The following code is for Edit/Delete (basically update operation) using DataGridView:

private void panel1_Paint(object sender, PaintEventArgs e)
{
/*this.flatDataBindingSource.EndEdit();
this.flatDataBindingSource.ResetBindings(true);
this.flat_DataTableAdapter.GetData();*/
//this.Validate();

this.flatDataBindingSource.ResetBindings(true);
this.flat_DataTableAdapter.GetData();
}

private void FlatDatadataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
/*
* Variables: selectedRowIndex holds the row which was selected using the Select Button
* DataGridViewRow class represents a row in the DataGrid.
* For more info on this class http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.aspx
* cellCollection-is an instance of the CellCollection class.It is created
* to hold the cell data of the corresponding row.
* For more info CellCollection class http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcellcollection_members.aspx
*/
int selectedRowIndex = -1;
DataGridViewRow selectedRow;
DataGridViewCellCollection cellCollection;
short key = -1;
//string query_string;
OleDbConnection oleconnection = new OleDbConnection(conn_str);
OleDbCommand command;

this.FlatDatadataGridView1.Update();
this.FlatDatadataGridView1.Refresh();

if (e.ColumnIndex == 7)
{
selectedRow = FlatDatadataGridView1.CurrentRow;
selectedRowIndex = selectedRow.Index;
cellCollection = selectedRow.Cells;
string s;
//s = Convert.ToString(cellCollection[2].Value);

s = "UPDATE Flat_Data SET Flat_Data.FK_Main_ID=" + Convert.ToString(cellCollection[1].Value) + ", FK_Level1_ID=" + Convert.ToString(cellCollection[2].Value) + ", FK_Level2_ID=" + Convert.ToString(cellCollection[3].Value) + ", FK_Level3_ID=" + Convert.ToString(cellCollection[4].Value) + ", FK_Sem_Yr_ID=" + Convert.ToString(cellCollection[5].Value) + ", Flat_Data.[Desc]=\"" + Convert.ToString(cellCollectionDevil.Value) + "\" Where (((Flat_Data.ID)=" + Convert.ToString(cellCollection[0].Value)+"))";
command = new OleDbCommand(s, oleconnection);
try
{
command.Connection.Open();
command.ExecuteNonQuery();

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (oleconnection.State == ConnectionState.Open)
oleconnection.Close();
}
//return cellCollection to Editing screen.

}
else if (e.ColumnIndex == 8)
{
selectedRow = FlatDatadataGridView1.CurrentRow;
selectedRowIndex = selectedRow.Index;
cellCollection = selectedRow.Cells;
key = Convert.ToInt16(selectedRow.Cells[0].Value);
string str;


str = "Delete * from Flat_Data Where ID=" + Convert.ToString(key);

command = new OleDbCommand(str,oleconnection);

try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (oleconnection.State == ConnectionState.Open)
oleconnection.Close();
}
// EditEntryPanel.Refresh();
/*Because of two copies of DB's everytime a record is
* deleted it is removed from the actual copy but still remains
* in the DB at bin/Debug and everytime you run the program
* then bin/Debug DB is updated.
*/
EditEntryPanel.Refresh();
FlatDatadataGridView1.Refresh();
}
}






 
 
RoscoMac





PostPosted: .NET Framework Data Access and Storage, Newly inserted record does not appear in my GUI. Top

Hey Preps, in building a gui its very tempting to use panels and use splitters sendToBack bringToFront methods to try and make a outlook type environment. This is cool for a few panels but can get messy, I would suggest you look at mdi's, but that wasnt your question.

I am assuming you are using datasets to move your data, the wizards allow you to add database functionality quickly, dragging components from data sources will bind the controls to the dataset automatically. If you browse the dataset you should see an update and fill method, you need to run fill method on the dataset when you wnat to view the changed rows. this will fill the your textboxes and datagridviews

cheers