0

Hi omoz4real.
In your code,i found that you hadn't opened your connection before executed a command,so you probably couldn't connect to the database.
If you didn't intend to execute a command which return a table as a result,you should use another way.There is xxCommand.ExecuteNonQuery().
I have a small attached program that clear your confuse.
Hope this useful.

You're program is doing EXACTLY what I need mine to do, but looking through your code I tried to change mine around so to work w/ my DB, and it's failing at the ExecuteNonQuery line.

string userName;
            string passWord;
            string verify;
            string streetAddress;
            string address2;
            string City;
            string State;
            int Zip;
            string phoneNumber;
            string fName;
            string lName;
            
            userName = txtUserName.Text;
            streetAddress = txtStreetAddress.Text;
            address2 = txtAddress2.Text;
            City = txtCity.Text;
            State = txtState.Text;
            try
            {
                Zip = Int32.Parse(txtZip.Text);
            }
            catch
            {
                MessageBox.Show("Zip Code must be a number", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            phoneNumber = txtPhoneNumber.Text;
            passWord = mtxtpassword.Text;
            verify = mtxtVerify.Text;
            fName = txtFirstName.Text;
            lName = txtLastName.Text;

            if (MAconn != null && MAconn.State == ConnectionState.Open)
            {
                MAconn.Close();
                MAcmd.Dispose();
            }

            try
            {
                MAconn = new OleDbConnection();
                MAconn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Geodude\\Documents\\Database1.mdb";
                MAconn.Open();
                MAcmd = MAconn.CreateCommand();
            }
            catch
            {
                MessageBox.Show("Database unable to be opened.");
                return;
            }

            MAcmd.CommandText = "INSERT INTO table1 VALUES('"+userName+"',"+ passWord+"',"+
                fName+"',"+ lName+"',"+ streetAddress+"',"+ address2+"',"+ City+"',"+ State+"',"+ Zip+"',"+ phoneNumber+"')";

            try
            {
                int row_affected = MAcmd.ExecuteNonQuery();
                MessageBox.Show(row_affected.ToString() + " row affected.", "Insert command result");
            }
            catch
            {
                MessageBox.Show("Error");
            }

this is my code, it is being used for a "Register" form, on a program I have to write to create a Ticketmaster esque program, and like I said you're program is doing EXACTLY what i've been searching the internet for like 4hrs and came up empty handed. If you could possibly tell me where I'm messing up I would really appreciate it.

1
Contributor
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Geodude0487
0

ok got and update for anybody that can help me, i added in an "ID" box b/c that is how you had yours and it works successfully. Is there a way that I can make this ID box work off of my "userName" variable so that as long as the username is different it'll add it? If not what is a good way to set up a counter so that every time it's loaded the counter for the ID updates appropriately?
Here is my updated code:

private void btnRegister_Click(object sender, EventArgs e)
        {
            string userName;
            string passWord;
            string verify;
            string streetAddress;
            string address2;
            string City;
            string State;
            int Zip;
            string phoneNumber;
            string fName;
            string lName;
            
            userName = txtUserName.Text;
            streetAddress = txtStreetAddress.Text;
            address2 = txtAddress2.Text;
            City = txtCity.Text;
            State = txtState.Text;
            try
            {
                Zip = Int32.Parse(txtZip.Text);
            }
            catch
            {
                MessageBox.Show("Zip Code must be a number", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            phoneNumber = txtPhoneNumber.Text;
            passWord = mtxtpassword.Text;
            verify = mtxtVerify.Text;
            fName = txtFirstName.Text;
            lName = txtLastName.Text;

            if (MAconn != null && MAconn.State == ConnectionState.Open)
            {
                MAconn.Close();
                MAcmd.Dispose();
            }

            try
            {
                MAconn = new OleDbConnection();
                MAconn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Geodude\\Documents\\Database1.mdb";
                MAconn.Open();
                MAcmd = MAconn.CreateCommand();
            }
            catch
            {
                MessageBox.Show("Database unable to be opened.");
                return;
            }

            MAcmd.CommandText = "INSERT INTO table1 VALUES('"+counter+"','"+"','"+ passWord+"','"+
                fName+"','"+ lName+"','"+ streetAddress+"','"+ address2+"','"+ City+"','"+ State+"','"+ Zip+"','"+ phoneNumber+"')";

            try
            {
                int row_affected = MAcmd.ExecuteNonQuery();
                MessageBox.Show(row_affected.ToString() + " row affected.", "Insert command result");
            }
            catch
            {
                MessageBox.Show("Error");
            }

            if (MAconn != null)
            {
                if (MAconn.State == ConnectionState.Open) MAconn.Close();
                MAconn.Dispose();
            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.