Currently, I have been doing some simple coding related to SQL and C#. I have created this a registration form which will store the data, username and password, in an SQLite database. This is the code that I am currently using:
private void AddLoginInfo(string username, string password)
{
auth = new Authentication();
auth.getConnection();
using (SQLiteConnection con = new SQLiteConnection(auth.connectionstring))
{
SQLiteCommand cmd = new SQLiteCommand();
con.Open();
string que = @"INSERT INTO LoginInfo(Username, Password) VALUES (@username, @password)";
cmd.CommandText = que;
cmd.Connection = con;
cmd.Parameters.Add(new SQLiteParameter("@Username", username));
cmd.Parameters.Add(new SQLiteParameter("@Password", password));
cmd.ExecuteNonQuery();
MessageBox.Show("Account Created!");
}
}
This code currently works and does add to the correct table in the database however whenever a new user is added. The user information can be used to login but it does not show within the table, which is viewed using DB Browser for SQLite.
For example, if I create a new user; 'admin' as the username and 'password' as the password, through the form, I get the message box saying 'Account Created' and I can use that very account to login. However, if I go view that very data in the DB browser the data doesn't show even after refreshing the table.
After doing some digging, I found this and saw that they were using sqlCommand.Parameters.AddWithValue
so I tried this within my code:
SQLiteCommand cmd = new SQLiteCommand(@"INSERT INTO LoginInfo(Username, Password) VALUES (@username, @password)", con);
cmd.Parameters.AddWithValue(new SQLiteParameter("@Username", txtBoxUsername.Text));
I tried this and I got a CS7036 error. Then I realised that they had not used the new SQLiteParameter()
part and so I removed it cmd.Parameters.AddWithValue("@Username", txtBoxUsername.Text);
and tried again but it still wouldn't update in the table but the data could still be used to log in.
I have also found a similar post but no one had answered it. Now I don't know what to do, so I am asking for your help.