CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    1

    c# database help

    I've been working on this for days. I can't seem to insert into my access db. here is my code. When the code is run, the "YEAH!!!" message is displayed, but no new record in the db. Any help would be fantastic.


    string conString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\BT1.mdb;providerName=System.Data.OleDb";
    OleDbConnection BoardConnection = new OleDbConnection(conString);

    try
    {

    OleDbCommand cmd = new OleDbCommand("INSERT INTO tblBoards (BoardName, BoardSerial, BoardOpened, EmpOpened, BoardPos) values (@BoardName, @BoardSerial, @BoardOpened, @EmpOpened, @BoardPos)", BoardConnection);



    cmd.Parameters.Add("@BoardName", OleDbType.VarChar).Value = varBoardNames;
    cmd.Parameters.Add("@BoardSerial", OleDbType.VarChar).Value = varBoardSers;
    cmd.Parameters.Add("@BoardOpened", OleDbType.DBDate).Value = varBoardOpen;
    cmd.Parameters.Add("@EmpOpened", OleDbType.VarChar).Value = "mike";
    cmd.Parameters.Add("@BoardPos", OleDbType.Integer).Value = varBoardPos;


    BoardConnection.Open();
    int numRows = cmd.ExecuteNonQuery();
    BoardConnection.Close();
    if (numRows == 1)
    {

    MessageBox.Show("YEAH!!!");
    }
    else
    {

    MessageBox.Show("****");
    }


    }
    catch (Exception e)
    { }

    finally
    {
    if (BoardConnection != null)
    {
    BoardConnection.Close();
    }
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: c# database help

    This doesn't return any rows
    Code:
    int numRows = cmd.ExecuteNonQuery();
    You'll have to check afterwords to see if its there.

    I wonder about the |Data Directory| statement
    Code:
    string conString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\BT1.mdb;providerName=System.Data.OleDb";
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured