Wednesday, 3 October 2012

How to Insert record into MS Access Data from ASP.NET

Resources:



protected void btnAdd_Click(object sender, EventArgs e)
    {
        string adminNumber = txtAdminNumber.Text.ToString();
        string surname = txtSurname.Text.ToString();
        string forname = txtForname.Text.ToString();
        //Boolean induction = true;
        Boolean induction = Convert.ToBoolean(ddlInduction.SelectedItem.Value.ToString());
        string called = "called";
        string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\GYMREG\App_Data\GymReg.accdb";
        OleDbConnection conn = new OleDbConnection(conString);
        string sql = "Insert into dbo_vwStudCYAll(AdmissionNumber, Surname, Forename, Called, Induction) values(?,?,?,?,?)";

        OleDbCommand comm = new OleDbCommand(sql, conn);
        comm.CommandType = CommandType.Text;
        comm.Parameters.AddWithValue("AdmisssionNumber", adminNumber);
        comm.Parameters.AddWithValue("Surname", surname);
        comm.Parameters.AddWithValue("Forename", forname);
        comm.Parameters.AddWithValue("Called", called);
        comm.Parameters.AddWithValue("Induction", induction);

        conn.Open();
        try
        {
            comm.ExecuteNonQuery();
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
    }

No comments:

Post a Comment