Again this is about a DataTable
, not a database.
Few things:
DataTable
is contained in its own class (MainDataTable.cs
)MainDataTable.cs (where the DataTable
is contained)
public class MainDataTable
{
public static DataTable dataMain = new DataTable("Customer Info Database");
public static void CreateTable1()
{
dataMain.Columns.Add("CustID", typeof(int));
dataMain.PrimaryKey = new DataColumn[] { dataMain.Columns["ID"] };
dataMain.Columns.Add("CustName", typeof(string));
dataMain.Columns.Add("CustAge", typeof(int));
dataMain.Columns.Add("CustAlign", typeof(string));
}
public static void EnterNewRows(int CustID, string CustName, int CustAge, string CustAlign)
{
dataMain.Rows.Add(CustID, CustName, CustAge, CustAlign);
}
}
Page1.xaml.cs
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void Finished_Button(object sender, RoutedEventArgs e)
{
int ParseID;
int ParseAge;
bool Parse1 = int.TryParse(txtID.Text, out ParseID);
bool Parse2 = int.TryParse(txtAge.Text, out ParseAge);
MainDataTable.CreateTable1();
MainDataTable.EnterNewRows(ParseID, txtName.Text, ParseAge, txtAlignment.Text);
this.NavigationService.Navigate(new Page0());
Page2.xaml.cs
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
}
private void SearchBtn1Clk(object sender, RoutedEventArgs e)
{
int IDFind = Convert.ToInt32(searchIdTxtBox.Text);
DataRow foundRow = MainDataTable.dataMain.Rows.Find(IDFind);
if (foundRow != null)
{
MessageBox.Show(foundRow[0].ToString());
}
else
{
MessageBox.Show("No Customer Found with ID:" + IDFind);
}
}
}
The values are stored in the table correctly from Page1.xaml.cs text input (I know this because I checked the rows after it ran), but when clicking search, this error occurs:
An unhandled exception of type 'System.Data.MissingPrimaryKeyException' occurred in System.Data.dll Additional information: Table doesn't have a primary key.
You don't have a column called ID
. It's called CustID
.Isn't it? Update this line and try again.
dataMain.PrimaryKey = new DataColumn[] { dataMain.Columns["CustID"] };
Unless you are trying to accomplish something else.