Greetings!
I was having trouble in getting a specific value from a column in my SQLite Database to my WPF C# application.
using System;
using System.Windows;
using System.Windows.Input;
using System.Data.SQLite;
namespace IEEE_UESB_Database
{
public partial class Window2 : Window
{
string dbConnectionString = @"Data Source=ieeeuesbmembers.sqlite;Version=3";
int memID;
public Window2()
{
InitializeComponent();
}
private void cmdAddNew_Click(object sender, RoutedEventArgs e)
{
memID++;
tbxMemID.Text = Convert.ToString(memID);
tbxLastName.Text = "";
tbxFirstName.Text = "";
tbxMiddleName.Text = "";
tbxNickName.Text = "";
tbxStudentNumber.Text = "";
cbxYearLevel.Text = "";
tbxContactNumber.Text = "";
tbxEmailAddress.Text = "";
cbxCourse.Text = "";
tbxAddress.Text = "";
cbxCommittee.Text = "";
cbxPosition.Text = "";
}
private void cmdDelete_Click(object sender, RoutedEventArgs e)
{
}
private void cmdSave_Click(object sender, RoutedEventArgs e)
{
SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
try
{
sqliteCon.Open();
string Query = "insert into ieeeuesbmembers (memberID,lastName,firstName,middleName,nickName,studentNumber,yearLevel,contactNumber,emailAddress,course,address,committee,position) values ('" + this.tbxMemID.Text + "','" + this.tbxLastName.Text + "','" + this.tbxFirstName.Text + "','" + this.tbxMiddleName.Text + "','" + this.tbxNickName.Text + "','" + this.tbxStudentNumber.Text + "','" + this.cbxYearLevel.Text + "','" + this.tbxContactNumber.Text + "','" + this.tbxEmailAddress.Text + "','" + this.cbxCourse.Text + "','" + this.tbxAddress.Text + "','" + this.cbxCommittee.Text + "','" + this.cbxPosition.Text + "')";
SQLiteCommand createCommand = new SQLiteCommand(Query, sqliteCon);
createCommand.ExecuteNonQuery();
MessageBox.Show("Record successfully saved!");
sqliteCon.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void cmdLogout_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Thank you for using this software!");
this.Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
try
{
sqliteCon.Open();
SQLiteCommand createCommand = new SQLiteCommand(sqliteCon);
SQLiteDataReader dataReader = createCommand.ExecuteReader();
memID = Convert.ToInt32(dataReader["select max(memID) from ieeeuesbmembers"]);
tbxMemID.Text = Convert.ToString(memID);
sqliteCon.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
My problem is on the Windows_Loaded event, everytime the application is opened, I wanted the tbxMemID to always have the max value that was recorded as a primary key with autoincrement function on the database, so that I won't overlap any records everytime I tried to add one.
It keeps giving this error message.
Value cannot be null.
Parameter: s
Can someone help me out?
Thanks!