I have a main form for adding and deleting records and a button that opens a child form that displays records from an access database. Child form will have buttons for moving though the rows like first, last, next, previous and search. When user selects a record the child form will close and the selected record will be displayed in the main form, from which it can be changed, deleted, etc. This is the logic behind what I’m doing. So far I have written a class for accessing the data and is called from the child form to display the records. Before I move on, I want to get advice on what I have done so far and if it follows best practices. Any advice would be greatly appreciated.
Main Form:
namespace Test
{
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
}
public class myClass
{
private static OleDbConnection myConn = new OleDbConnection();
private static DataSet myDS;
private static DataRow myDR;
public static int MaxRows = 0;
public static int increment = 0;
public static string name;
public static void GetConnection()
{
myClass.myConn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Temp\Testing\TestDatabase.accdb";
try
{
myConn.Open();
myDS = new DataSet();
string SQL = "SELECT * From Test";
OleDbDataAdapter myDA = new OleDbDataAdapter(SQL, myConn);
myDA.Fill(myDS, "People");
NavigateRecords();
MaxRows = myDS.Tables["People"].Rows.Count;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
myConn.Close();
}
}
public static void NavigateRecords()
{
myDR = myDS.Tables["People"].Rows[increment];
name = myDR.ItemArray.GetValue(1).ToString();
}
}
private void frmNewForm_Click(object sender, EventArgs e)
{
frmNewForm fNew = new frmNewForm();
fNew.ShowDialog();
}
}
}
Child form:
namespace Test
{
public partial class frmNewForm : Form
{
public frmNewForm()
{
InitializeComponent();
}
private void frmNewForm_Load(object sender, EventArgs e)
{
MyAlias.GetConnection();
txtName.Text = MyAlias.name;
}
private void FillTextBox()
{
txtName.Text = MyAlias.name;
}
private void btnNext_Click(object sender, EventArgs e)
{
if (MyAlias.increment != MyAlias.MaxRows - 1)
{
MyAlias.increment++;
MyAlias.NavigateRecords();
FillTextBox();
}
else
{
MessageBox.Show("No more rows");
}
}
}
}