0

I have created a web page in c# and i am attempting to save the data from the web page to my ms database. There are no errors in the code however it is not saving to the database, can someone take a look at this and see whats wrong please??

this is my code behind

    public partial class Checklist : System.Web.UI.Page
     {
      OleDbCommand cmd = new OleDbCommand();
      OleDbConnection conn = new OleDbConnection();
      OleDbDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
     {
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data      Source=C:\\Users\\IFMComac\\Documents\\Visual Studio 2013\\ifm.mdb.accdb";
        cmd.Connection = conn;
     }//end of page load

    protected void cmdSave_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\IFMComac\\Documents\\Visual Studio 2013\\ifm.mdb.accdb";
        conn.Open();

        string cmdString = ("INSERT INTO tblCheck (ChecklistID, Vehicle, Driver, WeekEnding, FuelLevel, WindscreenWasher, SteeringWheel,Brakes, Clutch, Horn, Heater, SeatBelts, WarningLights, Mirrors, Tires/Wheels, Exhaust, Lights/Reflectors, ExteriorLeaks, Body, OilLevel, CoolantLevel, Belts, EngineLeaks, LooseBolts/Screws, WarningTriangle, FireExtinguishers/FirstAidKit, AdditionalComments, CFirstName, CLastName)" + " Values (@CheckID, @Vehicle, @Driver, @WeekEnding, @FuelLevel, @WindscreenWasher, @SteeringWheel, @Brakes, @Clutch, @Horn, @Heater, @SeatBelts, @WarningLights, @Mirrors, @Tires/Wheels, @Exhaust, @Lights/Reflectors, @ExteriorLights, @Body, @OilLevel, @CoolantLevel, @Belts, @EngineLeaks, @LooseBolts/Screws, @WarningTriangle, @FireExtinguishers/FirstAidKit, @AdditionalComments, @CFirstName, @CLastName)");
        OleDbCommand cmd = new OleDbCommand(cmdString, conn);

        if (conn.State == ConnectionState.Open)
        {
            cmd.Parameters.Add("@CheckID", OleDbType.Integer, 5).Value = txtID;
            cmd.Parameters.Add(@"Vehicle", OleDbType.VarWChar, 10).Value = ddlReg;
            cmd.Parameters.Add(@"Driver", OleDbType.VarWChar, 30).Value = ddlReg;
            cmd.Parameters.Add(@"WeekEnding", OleDbType.Date, 40).Value = cldDate0;
            cmd.Parameters.Add(@"FuelLevel", OleDbType.Boolean, 40).Value = chkbxSelect1;
            cmd.Parameters.Add(@"WindscreenWasher", OleDbType.Boolean, 40).Value = chkbxSelect6;
            cmd.Parameters.Add(@"SteeringWheel", OleDbType.Boolean, 40).Value = chkbxSelect11;
            cmd.Parameters.Add(@"Brakes", OleDbType.Boolean, 40).Value = chkbxSelect16;
            cmd.Parameters.Add(@"Clutch", OleDbType.Boolean, 40).Value = chkbxSelect21;
            cmd.Parameters.Add(@"Horn", OleDbType.Boolean, 40).Value = chkbxSelect26;
            cmd.Parameters.Add(@"Heater", OleDbType.Boolean, 40).Value = chkbxSelect31;
            cmd.Parameters.Add(@"SeatBelts" , OleDbType.Boolean, 40).Value = chkbxSelect36;
            cmd.Parameters.Add(@"WarningLights" , OleDbType.Boolean, 40).Value = chkbxSelect41;
            cmd.Parameters.Add(@"Mirrors", OleDbType.Boolean, 40).Value = chkbxSelect46;
            cmd.Parameters.Add(@"Tires/Wheels", OleDbType.Boolean, 40).Value = chkbxSelect51;
            cmd.Parameters.Add(@"Exhaust", OleDbType.Boolean, 40).Value = chkbxSelect56;
            cmd.Parameters.Add(@"Lights/Reflectors", OleDbType.Boolean, 40).Value = chkbxSelect61;
            cmd.Parameters.Add(@"Exterior Leaks", OleDbType.Boolean, 40).Value = chkbxSelect66;
            cmd.Parameters.Add(@"Body", OleDbType.Boolean, 40).Value = chkbxSelect71;
            cmd.Parameters.Add(@"OilLevel", OleDbType.Boolean, 40).Value = chkbxSelect76;
            cmd.Parameters.Add(@"CoolantLevel", OleDbType.Boolean, 40).Value = chkbxSelect81;
            cmd.Parameters.Add(@"Belts", OleDbType.Boolean, 40).Value = chkbxSelect86;
            cmd.Parameters.Add(@"EngineLeaks", OleDbType.Boolean, 40).Value = chkbxSelect91;
            cmd.Parameters.Add(@"LooseBolts/Screws", OleDbType.Boolean, 40).Value = chkbxSelect96;
            cmd.Parameters.Add(@"WarningTriangle", OleDbType.Boolean, 40).Value = chkbxSelect101;
            cmd.Parameters.Add(@"FireExtinguisher/FirstAidKit", OleDbType.Boolean, 40).Value = chkbxSelect106;
            cmd.Parameters.Add(@"AdditionalComments", OleDbType.VarWChar, 100).Value = addCom;
            cmd.Parameters.Add(@"CFirstName", OleDbType.VarWChar, 30).Value = txtFirstName;
            cmd.Parameters.Add(@"CLastName", OleDbType.VarWChar, 30).Value = txtLastName;


        }
        else
        {
            conn.Close();
        }

    }
}

}

When i try to retrieve the data from the database it is empty. is there something missing??

5
  • 3
    what about reading some books before asking this question? Commented Apr 15, 2014 at 11:05
  • why bother to write on this if your not going to be helpful Commented Apr 15, 2014 at 11:16
  • true! but there are some many related questions around here, some many OleDb samples... so many! Commented Apr 15, 2014 at 11:19
  • yes and I have looked around and tried all different examples, so whats wrong with asking for help isnt that what these forums are for Commented Apr 15, 2014 at 11:24
  • @user3432846 to answer your last comment, this isn't a forum. Can I suggest using a try catch block in this code where there should be one anyway. Commented Apr 15, 2014 at 16:04

2 Answers 2

1

cmd.ExecuteNonQuery(); statement is missing.

Sign up to request clarification or add additional context in comments.

Comments

1

try this

    protected void cmdSave_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\IFMComac\\Documents\\Visual Studio 2013\\ifm.mdb.accdb";

        string cmdString = ("INSERT INTO tblCheck (ChecklistID, Vehicle, Driver, WeekEnding, FuelLevel, WindscreenWasher, SteeringWheel,Brakes, Clutch, Horn, Heater, SeatBelts, WarningLights, Mirrors, Tires/Wheels, Exhaust, Lights/Reflectors, ExteriorLeaks, Body, OilLevel, CoolantLevel, Belts, EngineLeaks, LooseBolts/Screws, WarningTriangle, FireExtinguishers/FirstAidKit, AdditionalComments, CFirstName, CLastName)" + " Values (@CheckID, @Vehicle, @Driver, @WeekEnding, @FuelLevel, @WindscreenWasher, @SteeringWheel, @Brakes, @Clutch, @Horn, @Heater, @SeatBelts, @WarningLights, @Mirrors, @Tires/Wheels, @Exhaust, @Lights/Reflectors, @ExteriorLights, @Body, @OilLevel, @CoolantLevel, @Belts, @EngineLeaks, @LooseBolts/Screws, @WarningTriangle, @FireExtinguishers/FirstAidKit, @AdditionalComments, @CFirstName, @CLastName)");
        OleDbCommand cmd = new OleDbCommand(cmdString, conn);

        if (conn.State == ConnectionState.Closed)
        {
             conn.Open();
        }


cmd.Parameters.Add("@CheckID", OleDbType.Integer, 5).Value = txtID;
        cmd.Parameters.Add(@"Vehicle", OleDbType.VarChar, 10).Value = ddlReg;
        cmd.Parameters.Add(@"Driver", OleDbType.VarChar, 30).Value = ddlReg;
        cmd.Parameters.Add(@"WeekEnding", OleDbType.Date, 40).Value = cldDate0;
        cmd.Parameters.Add(@"FuelLevel", OleDbType.Boolean, 40).Value = chkbxSelect1;
        cmd.Parameters.Add(@"WindscreenWasher", OleDbType.Boolean, 40).Value = chkbxSelect6;
        cmd.Parameters.Add(@"SteeringWheel", OleDbType.Boolean, 40).Value = chkbxSelect11;
        cmd.Parameters.Add(@"Brakes", OleDbType.Boolean, 40).Value = chkbxSelect16;
        cmd.Parameters.Add(@"Clutch", OleDbType.Boolean, 40).Value = chkbxSelect21;
        cmd.Parameters.Add(@"Horn", OleDbType.Boolean, 40).Value = chkbxSelect26;
        cmd.Parameters.Add(@"Heater", OleDbType.Boolean, 40).Value = chkbxSelect31;
        cmd.Parameters.Add(@"SeatBelts" , OleDbType.Boolean, 40).Value = chkbxSelect36;
        cmd.Parameters.Add(@"WarningLights" , OleDbType.Boolean, 40).Value = chkbxSelect41;
        cmd.Parameters.Add(@"Mirrors", OleDbType.Boolean, 40).Value = chkbxSelect46;
        cmd.Parameters.Add(@"Tires/Wheels", OleDbType.Boolean, 40).Value = chkbxSelect51;
        cmd.Parameters.Add(@"Exhaust", OleDbType.Boolean, 40).Value = chkbxSelect56;
        cmd.Parameters.Add(@"Lights/Reflectors", OleDbType.Boolean, 40).Value = chkbxSelect61;
        cmd.Parameters.Add(@"Exterior Leaks", OleDbType.Boolean, 40).Value = chkbxSelect66;
        cmd.Parameters.Add(@"Body", OleDbType.Boolean, 40).Value = chkbxSelect71;
        cmd.Parameters.Add(@"OilLevel", OleDbType.Boolean, 40).Value = chkbxSelect76;
        cmd.Parameters.Add(@"CoolantLevel", OleDbType.Boolean, 40).Value = chkbxSelect81;
        cmd.Parameters.Add(@"Belts", OleDbType.Boolean, 40).Value = chkbxSelect86;
        cmd.Parameters.Add(@"EngineLeaks", OleDbType.Boolean, 40).Value = chkbxSelect91;
        cmd.Parameters.Add(@"LooseBolts/Screws", OleDbType.Boolean, 40).Value = chkbxSelect96;
        cmd.Parameters.Add(@"WarningTriangle", OleDbType.Boolean, 40).Value = chkbxSelect101;
        cmd.Parameters.Add(@"FireExtinguisher/FirstAidKit", OleDbType.Boolean, 40).Value = chkbxSelect106;
        cmd.Parameters.Add(@"AdditionalComments", OleDbType.VarChar, 100).Value = addCom;
        cmd.Parameters.Add(@"CFirstName", OleDbType.VarChar, 30).Value = txtFirstName;
        cmd.Parameters.Add(@"CLastName", OleDbType.VarChar, 30).Value = txtLastName;
        cmd.ExecuteNonQuery();
        conn.Close();

    }

Without ExecuteNonQuery() your query is not executed hence your data is not saved in database.

4 Comments

its throwing up and error when i add ExecuteNonQuery
System.InvalidCastException: Object must implement IConvertible. Source Error: Line 74: cmd.Parameters.Add(@"CFirstName", OleDbType.VarWChar, 30).Value = txtFirstName; Line 75: cmd.Parameters.Add(@"CLastName", OleDbType.VarWChar, 30).Value = txtLastName; Line 76: cmd.ExecuteNonQuery(); Line 77: Line 78: }
I prefer you to READ SOME BOOKS before starting
Change VarWChar to Varchar. take advantage of intellisence help of visual studio.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.