Reputation: 31
I have been working on this a while. I'm attempting to add values to an access DB through a custom program. I don't have any syntax errors or anything that I can see wrong, but i'm a rookie. Any help would be much appreciated. I have some dirty code in there I was messing around with, but commented it out. Please help! Thanks guys and gals.
I'm on AMD Win 10 Visual Studio 2015 Access 2016
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmNewClient : Form
{
OleDbConnection connect = new OleDbConnection();
public frmNewClient()
{
InitializeComponent();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void cmdClientInput_Click(object sender, EventArgs e)
{
string CompanyName = txtCCOMPANYNAME.Text;
string ContactName = txtCCONTACTNAME.Text;
string Street = txtCSTREET.Text;
string City = txtCCITY.Text;
string State = txtCSTATE.Text;
string ZIP = txtCZIP.Text;
string Phone1 = txtCPHONE1.Text;
string Phone2 = txtCPHONE2.Text;
string Email = txtCEMAIL.Text;
string Notes = txtCNOTES.Text;
connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\lafiammadb.accdb;Persist Security Info=False";
OleDbCommand cmd = new OleDbCommand("INSERT Into Clients(CompanyName, ContactName, Street, City, State, ZIP, Phone1, Phone2, Email, Notes)" + "values(@CompanyName, @ContactName, @Street, @City, @State, @ZIP, @Phone1, @Phone2, @Email, @Notes)", connect);
if (connect.State == ConnectionState.Open)
{
cmd.Parameters.Add("@CompanyName", OleDbType.Char, 20).Value = CompanyName;
cmd.Parameters.Add("@ContactName", OleDbType.Char, 20).Value = ContactName;
cmd.Parameters.Add("@Street", OleDbType.Char, 20).Value = Street;
cmd.Parameters.Add("@City", OleDbType.Char, 20).Value = City;
cmd.Parameters.Add("@State", OleDbType.Char, 20).Value = State;
cmd.Parameters.Add("@ZIP", OleDbType.Char, 20).Value = ZIP;
cmd.Parameters.Add("@Phone1", OleDbType.Char, 20).Value = Phone1;
cmd.Parameters.Add("@Phone2", OleDbType.Char, 20).Value = Phone2;
cmd.Parameters.Add("@Email", OleDbType.Char, 20).Value = Email;
cmd.Parameters.Add("@Notes", OleDbType.Char, 20).Value = Notes;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Data Added To Database");
txtCCOMPANYNAME.Text = "";
txtCCONTACTNAME.Text = "";
txtCSTREET.Text = "";
txtCCITY.Text = "";
txtCSTATE.Text = "";
txtCZIP.Text = "";
txtCPHONE1.Text = "";
txtCPHONE2.Text = "";
txtCEMAIL.Text = "";
txtCNOTES.Text = "";
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
connect.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
//connect.Open();
//OleDbCommand cmd = new OleDbCommand(q);
//cmd.Parameters.AddWithValue("@CompanyName", CompanyName);
//cmd.Parameters.AddWithValue("@ContactName", ContactName);
//cmd.Parameters.AddWithValue("@Street", Street);
//cmd.Parameters.AddWithValue("@City", City);
//cmd.Parameters.AddWithValue("@ZIP", ZIP);
//cmd.Parameters.AddWithValue("@Phone1", Phone1);
//cmd.Parameters.AddWithValue("@Phone2", Phone2);
//cmd.Parameters.AddWithValue("@Email", Email);
//cmd.Parameters.AddWithValue("@Notes", Notes);
//cmd.ExecuteNonQuery();
//OleDbCommand CmdSql = new OleDbCommand("Insert into [clients](Company Name, Contact Name, Street, City, State, Zip, Phone1, Phone2, Email, Notes)
}
}
}
Upvotes: 3
Views: 632
Reputation: 6866
This line is giving you the problem if (connect.State == ConnectionState.Open)
. What you really want to do is to check if the connection is closed and then open it so:
if (connect.State == ConnectionState.Closed)
{
connect.Open();
//Do Some work
}
However I would recommend you make the use of you using
block this will ensure the object is disposed correctly.
From MSDN
To ensure that connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.
using(OleDbConnection connect = new OleDbConnection(connectionString))
{
connect.Open();
//Do some work
}// Here it will automatically call Dispose();
As OleDbConnection class implements IDisposable. So you can use Dispose()
method directly and won't be required to call connect.Close();
Upvotes: 0
Reputation: 172588
The issue is here:
if (connect.State == ConnectionState.Open)
You are checking that the connection is Open or not but at no point you are actually opening your connection. You need to Open
your connection before checking it. You can try like this:
if (connect.State == ConnectionState.Closed)
{
connect.Open();
}
On a side note:
You can think of using the using
statament as well when dealing with connections.
Upvotes: 2