Hello there.
I've put together an Access Database which handles opening hours.
However I want to be able to update each day individually, but at the moment it updates the entire week in one go.
I know that I'm supposed to add some sort of WHERE in my sql sentence, but I can't wrap my brain around the syntax for it...
Parts of the code is in danish, since my education requires it (stupid I know) let me know if you want me to translate it to english words, so it's easier to get an overview.

The database has two colums. One with the seven days of the week, and one with opening hours.

The class is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Web.Configuration;

namespace AabningstiderClass
{
    /// <summary>
    /// Summary description for aabningsTider
    /// </summary>
    public class aabningsTider
    {
        private string mandag;
        private string tirsdag;


        public aabningsTider()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string Mandag
        {
            set { mandag = value; }
            get { return mandag; }
        }
        public string Tirsdag
        {
            set { tirsdag = value; }
            get { return tirsdag; } 
        }

        //Opdater til database
        public string OpdaterTid()
        {
            string strSQL;
            strSQL = "UPDATE aabningsTabel SET aabningsTid = '" + this.Mandag.ToString() + "'";
          
            OleDbConnection myConnection = new OleDbConnection();
            myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|aabningstider.mdb";

            try
            {
                myConnection.Open();
                OleDbCommand myCommand = new OleDbCommand(strSQL, myConnection);
                myCommand.ExecuteNonQuery();
                return "Der skete en fejl i opdateringen";

            }
            catch (Exception ex)
            {
                string strFejl;
                strFejl = "Databasen blev ikke åbnet";
                strFejl += ex.ToString();
                return strFejl;
            }
            finally
            {
                myConnection.Close();
            }

        }

    }
}

And the execution:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AabningstiderClass;

public partial class aabning2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btGem_Click(object sender, EventArgs e)
    {
        aabningsTider opdaterTid = new aabningsTider();
        opdaterTid.Mandag = tbMandag.Text;
        opdaterTid.OpdaterTid();
    }
}

I hope you got some pointers :)
Thanks in advance!

UPDATE tablename SET columnname1='value' WHERE columnname2='value';

If the value is numeric, leave off the ticks ('...') around the value.

UPDATE tablename SET columnname1='value' WHERE columnname2='value';

If the value is numeric, leave off the ticks ('...') around the value.

Thank you SO much, that was exactly what I needed!

I only have one problem now though... I want t be able to update alle of the days in one go..
But this way won't work:

strSQL = "UPDATE aabningsTabel SET aabningsTid = '" + this.Mandag.ToString() + "' WHERE aabningsDag='Mandag'";
            strSQL = "UPDATE aabningsTabel SET aabningsTid = '" + this.Tirsdag.ToString() + "' WHERE aabningsDag='Tirsdag'";

Neither will:

strSQL = "UPDATE aabningsTabel SET aabningsTid (Mandag, Tirsdag) ";
            strSQL += "Values ('" + this.Mandag + "' , '" + this.Tirsdag + "')";

If I use the first way around, it will only update Tirsdag(Tuesday) as it is the second one to come... if I wrap a comment around Tirsdag it will update Mandag(Monday) just fine.
Any ideas?

nvm, I found the error...
I had to open a new OleDBCommand of course :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.