Hi I'm not sure if the answer is anywhere else in the forums but I have yet to find it. I'm working on a school project where we basically have to write a TicketMaster esque program. We have a main form, with a log-in form, and a register form. We need to be able to store the username and passwords in the access database so that our login form can function properly. I have my DB connected to the database but I don't know how to add a new user row to the database and have it actually save to the database so that when i try to access it from another form the new data is there and usable. If somebody could help me or direct me to the proper place to answer my question I would greatly appreciate it. Also I'm using Visual C# Express 2008.

Recommended Answers

All 3 Replies

Let me show your code.

This code insert a record into the table Emp. Take a look at this code.

System.Data.OleDb.OleDbConnection cn;
System.Data.OleDb.OleDbCommand cmd;

cn.ConnectionString="put_connectionstring_here";

cmd.CommandText="insert into emp values (@eno,@ename,@edate)";
cmd.Connection=cn;

cmd.Parameters.AddWithValue("@eno",textBox1.text);
cmd.Parameters.AddWithValue("@ename",textBox2.text);
cmd.Parameters.AddWithValue("@edate",textBox3.text);

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

ok I got my adding to the database portion to work properly, now the last thing I need to truly make my log-in program work the way I want it to is that upon logging in I want the "log-in" label on the main form to change from "Login" to "Welcome [username] Logout?" which I can get it to change but I don't know how to get the proper username to transfer with it. Here is the code for my log-in button on the log-in form. (also is there a way to change the source on Line 3 "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\John\\Desktop\\Database1.mdb" so that instead of being a direct link it's embedded into the program so I can submit it to my teacher?

private void btnLogIn_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\John\\Desktop\\Database1.mdb");
            OleDbCommand com = new OleDbCommand("SELECT UserName, PassWord FROM Table1",con);
            OleDbDataReader reader;
            
            bool permit = false;

            try
            {
                con.Open();
                reader = com.ExecuteReader();
                while (reader.Read())
                {
                    if (txtUserName.Text == (reader["UserName"].ToString()) && mtxtPassWord.Text == reader["PassWord"].ToString())
                    {
                        permit = true;
                        break;
                    }
                }

                con.Close();
            }
            catch
            {
                MessageBox.Show("File not able to be read");
                return;
            }

            if (permit != true)
                MessageBox.Show("Username or Password are not correct.");
            else
            {
                
                Close();
            }
                
        }

and here is the code for my Main form for the log-in link label

private void LnklblLogIn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            sp.Stop();
            frmLogIn LogIn = new frmLogIn();
            frmMainForm main = new frmMainForm();
            LogIn.Show();
           
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.