Decrement value by 1 MS Access SQL and C# [duplicate]










-1
















This question already has an answer here:



  • How can I add user-supplied input to an SQL statement?

    2 answers



I'm facing a problem with decrementing value with MS Access database.



I get an error




Syntax error in UPDATE Statement




My code:



connection.Open();
command = new OleDbCommand();
command.Connection = connection;
command.CommandText = " update Cards set Count = Count - 1 where Type=" + ct + " ";
command.ExecuteNonQuery();
connection.Close();


Can anyone please help?










share|improve this question















marked as duplicate by LarsTech c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 17:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • what is the Type datatype?

    – Ashkan Mobayen Khiabani
    Nov 13 '18 at 17:02











  • Use parameters to avoid sql injection and to fix your problem.

    – LarsTech
    Nov 13 '18 at 17:03












  • The Type is text

    – Omar
    Nov 13 '18 at 17:07











  • parameters not solving the problem I tried

    – Omar
    Nov 13 '18 at 17:21






  • 1





    @themehio Stop it. Parameters is the only "safe way" to fix this mistake. If you submit code like that at a job interview, you aren't getting the job.

    – LarsTech
    Nov 13 '18 at 17:29















-1
















This question already has an answer here:



  • How can I add user-supplied input to an SQL statement?

    2 answers



I'm facing a problem with decrementing value with MS Access database.



I get an error




Syntax error in UPDATE Statement




My code:



connection.Open();
command = new OleDbCommand();
command.Connection = connection;
command.CommandText = " update Cards set Count = Count - 1 where Type=" + ct + " ";
command.ExecuteNonQuery();
connection.Close();


Can anyone please help?










share|improve this question















marked as duplicate by LarsTech c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 17:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • what is the Type datatype?

    – Ashkan Mobayen Khiabani
    Nov 13 '18 at 17:02











  • Use parameters to avoid sql injection and to fix your problem.

    – LarsTech
    Nov 13 '18 at 17:03












  • The Type is text

    – Omar
    Nov 13 '18 at 17:07











  • parameters not solving the problem I tried

    – Omar
    Nov 13 '18 at 17:21






  • 1





    @themehio Stop it. Parameters is the only "safe way" to fix this mistake. If you submit code like that at a job interview, you aren't getting the job.

    – LarsTech
    Nov 13 '18 at 17:29













-1












-1








-1









This question already has an answer here:



  • How can I add user-supplied input to an SQL statement?

    2 answers



I'm facing a problem with decrementing value with MS Access database.



I get an error




Syntax error in UPDATE Statement




My code:



connection.Open();
command = new OleDbCommand();
command.Connection = connection;
command.CommandText = " update Cards set Count = Count - 1 where Type=" + ct + " ";
command.ExecuteNonQuery();
connection.Close();


Can anyone please help?










share|improve this question

















This question already has an answer here:



  • How can I add user-supplied input to an SQL statement?

    2 answers



I'm facing a problem with decrementing value with MS Access database.



I get an error




Syntax error in UPDATE Statement




My code:



connection.Open();
command = new OleDbCommand();
command.Connection = connection;
command.CommandText = " update Cards set Count = Count - 1 where Type=" + ct + " ";
command.ExecuteNonQuery();
connection.Close();


Can anyone please help?





This question already has an answer here:



  • How can I add user-supplied input to an SQL statement?

    2 answers







c# sql ms-access






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:16









marc_s

574k12811091256




574k12811091256










asked Nov 13 '18 at 17:00









OmarOmar

14




14




marked as duplicate by LarsTech c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 17:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by LarsTech c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 17:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • what is the Type datatype?

    – Ashkan Mobayen Khiabani
    Nov 13 '18 at 17:02











  • Use parameters to avoid sql injection and to fix your problem.

    – LarsTech
    Nov 13 '18 at 17:03












  • The Type is text

    – Omar
    Nov 13 '18 at 17:07











  • parameters not solving the problem I tried

    – Omar
    Nov 13 '18 at 17:21






  • 1





    @themehio Stop it. Parameters is the only "safe way" to fix this mistake. If you submit code like that at a job interview, you aren't getting the job.

    – LarsTech
    Nov 13 '18 at 17:29

















  • what is the Type datatype?

    – Ashkan Mobayen Khiabani
    Nov 13 '18 at 17:02











  • Use parameters to avoid sql injection and to fix your problem.

    – LarsTech
    Nov 13 '18 at 17:03












  • The Type is text

    – Omar
    Nov 13 '18 at 17:07











  • parameters not solving the problem I tried

    – Omar
    Nov 13 '18 at 17:21






  • 1





    @themehio Stop it. Parameters is the only "safe way" to fix this mistake. If you submit code like that at a job interview, you aren't getting the job.

    – LarsTech
    Nov 13 '18 at 17:29
















what is the Type datatype?

– Ashkan Mobayen Khiabani
Nov 13 '18 at 17:02





what is the Type datatype?

– Ashkan Mobayen Khiabani
Nov 13 '18 at 17:02













Use parameters to avoid sql injection and to fix your problem.

– LarsTech
Nov 13 '18 at 17:03






Use parameters to avoid sql injection and to fix your problem.

– LarsTech
Nov 13 '18 at 17:03














The Type is text

– Omar
Nov 13 '18 at 17:07





The Type is text

– Omar
Nov 13 '18 at 17:07













parameters not solving the problem I tried

– Omar
Nov 13 '18 at 17:21





parameters not solving the problem I tried

– Omar
Nov 13 '18 at 17:21




1




1





@themehio Stop it. Parameters is the only "safe way" to fix this mistake. If you submit code like that at a job interview, you aren't getting the job.

– LarsTech
Nov 13 '18 at 17:29





@themehio Stop it. Parameters is the only "safe way" to fix this mistake. If you submit code like that at a job interview, you aren't getting the job.

– LarsTech
Nov 13 '18 at 17:29












2 Answers
2






active

oldest

votes


















0














It's not certain, but I strongly suspect it's missing single quotes around ct. Fix it like this:



using (var connection = new OleDbConnection("connection string here"))
using (var command = new OleDbCommand("update Cards set Count = Count - 1 where Type= ?", connection))

//have to guess at the OleDbType value. Use the actual column type and length from the database
cmd.Parameters.Add("?", OleDbType.VarWChar, 10).Value = ct;
connection.Open();
command.ExecuteNonQuery();



There are several other important fixes in this pattern, too.






share|improve this answer






























    0














    You should provide an actual error.
    My guess is that count is a keyword and has to be put in square brackets like so [count]
    and do use parameters, see Joel's answer






    share|improve this answer























    • count is numeric not key word

      – Omar
      Nov 13 '18 at 17:19


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It's not certain, but I strongly suspect it's missing single quotes around ct. Fix it like this:



    using (var connection = new OleDbConnection("connection string here"))
    using (var command = new OleDbCommand("update Cards set Count = Count - 1 where Type= ?", connection))

    //have to guess at the OleDbType value. Use the actual column type and length from the database
    cmd.Parameters.Add("?", OleDbType.VarWChar, 10).Value = ct;
    connection.Open();
    command.ExecuteNonQuery();



    There are several other important fixes in this pattern, too.






    share|improve this answer



























      0














      It's not certain, but I strongly suspect it's missing single quotes around ct. Fix it like this:



      using (var connection = new OleDbConnection("connection string here"))
      using (var command = new OleDbCommand("update Cards set Count = Count - 1 where Type= ?", connection))

      //have to guess at the OleDbType value. Use the actual column type and length from the database
      cmd.Parameters.Add("?", OleDbType.VarWChar, 10).Value = ct;
      connection.Open();
      command.ExecuteNonQuery();



      There are several other important fixes in this pattern, too.






      share|improve this answer

























        0












        0








        0







        It's not certain, but I strongly suspect it's missing single quotes around ct. Fix it like this:



        using (var connection = new OleDbConnection("connection string here"))
        using (var command = new OleDbCommand("update Cards set Count = Count - 1 where Type= ?", connection))

        //have to guess at the OleDbType value. Use the actual column type and length from the database
        cmd.Parameters.Add("?", OleDbType.VarWChar, 10).Value = ct;
        connection.Open();
        command.ExecuteNonQuery();



        There are several other important fixes in this pattern, too.






        share|improve this answer













        It's not certain, but I strongly suspect it's missing single quotes around ct. Fix it like this:



        using (var connection = new OleDbConnection("connection string here"))
        using (var command = new OleDbCommand("update Cards set Count = Count - 1 where Type= ?", connection))

        //have to guess at the OleDbType value. Use the actual column type and length from the database
        cmd.Parameters.Add("?", OleDbType.VarWChar, 10).Value = ct;
        connection.Open();
        command.ExecuteNonQuery();



        There are several other important fixes in this pattern, too.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 17:05









        Joel CoehoornJoel Coehoorn

        307k95490721




        307k95490721























            0














            You should provide an actual error.
            My guess is that count is a keyword and has to be put in square brackets like so [count]
            and do use parameters, see Joel's answer






            share|improve this answer























            • count is numeric not key word

              – Omar
              Nov 13 '18 at 17:19
















            0














            You should provide an actual error.
            My guess is that count is a keyword and has to be put in square brackets like so [count]
            and do use parameters, see Joel's answer






            share|improve this answer























            • count is numeric not key word

              – Omar
              Nov 13 '18 at 17:19














            0












            0








            0







            You should provide an actual error.
            My guess is that count is a keyword and has to be put in square brackets like so [count]
            and do use parameters, see Joel's answer






            share|improve this answer













            You should provide an actual error.
            My guess is that count is a keyword and has to be put in square brackets like so [count]
            and do use parameters, see Joel's answer







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 17:07









            Daniel NDaniel N

            967




            967












            • count is numeric not key word

              – Omar
              Nov 13 '18 at 17:19


















            • count is numeric not key word

              – Omar
              Nov 13 '18 at 17:19

















            count is numeric not key word

            – Omar
            Nov 13 '18 at 17:19






            count is numeric not key word

            – Omar
            Nov 13 '18 at 17:19




            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Museum of Modern and Contemporary Art of Trento and Rovereto

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully