pls help me.. i have a program that have a database in ms access.. i haved already connect itto my ms access database but i cant save my data in my database everytime i click on my save button.. pls help me to know what codew will i write to save the data i write on a necessary field.. thank you very much..

this is my code sir Jx... tnx 4 ur support pls help me..:(

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    string stcron= "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Database\\db1.mdb;Persist Security Info=False"; 
    System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
    System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
    System.Data.OleDb.OleDbCommand dc = new System.Data.OleDb.OleDbCommand();


    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new System.Data.OleDb.OleDbConnection(stcron);
        DataTable dt = new DataTable();

    }

    protected void saveButton_Click(object sender, EventArgs e)
    {
      //  cn.Open();
       // dc = cn.CreateCommand();
      // dc.CommandText=


    }
}

pls help me sir..
my database is on ms access named db1 and my table name is tblUser.
the software that i am using is microsoft visual studio 2005 and im in c#.. pls help me sir jx_Man.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I've always used the following method for database operations. I split the actual database access into two methods: ExecuteQuery and ExecuteNonQuery. In your case, if you're trying to write to the database, you would want to execute a non-query.

private static void ExecuteNonQuery(OleDbCommand cmd)
{
        OleDbConnection dbConnection = new OleDbConnection(connection_string);

        cmd.Connection = dbConnection;
        dbConnection.Open();
        try
        {
            cmd.ExecuteNonQuery();
        }
        finally
        {
            dbConnection.Close();
        }
}

Putting it in it's own method let's you use it for any SQL statements that might need to execute a non-query. Then, in your save button code, you might want to do something like the following:

public static void insertRow(string value)
{
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO table_name(field_name) VALUES([@value]);";

        cmd.Parameters.AddWithValue("@value", value);

        ExecuteNonQuery(cmd);
}

Also, if you add a "using" clause for System.Data.OleDb, you wouldn't have to write so much in your code for those objects.

This article has been dead for over six months. Start a new discussion instead.