Problem with AddWithValue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SLIMSHIM

    #1

    Problem with AddWithValue

    Hi,
    I"m trying to insert into a database with no adapter .(AddWithValue) I get
    an error when
    executing ExcecuteNonQuer y . Parameter pName has no default value. what am I
    doing wrong ?

    using (OleDbConnectio n conn = new OleDbConnection (

    testacc.Propert ies.Settings.De fault.mytestCon nectionString))
    {

    using (OleDbCommand comm = new OleDbCommand(
    "INSERT INTO phonebook (name, phonenum) VALUES
    (pName, pPhonenum)", conn))
    {
    comm.Parameters .Add("pName",
    OleDbType.VarCh ar).SourceColum n = "name";

    comm.Parameters .Add("pPhonenum ",
    OleDbType.VarCh ar).SourceColum n = "phonenum";
    comm.Parameters .AddWithValue(" pName", "John Doe");
    comm.Parameters .AddWithValue(" pPhonenum",
    "555-555-1212");
    conn.Open();
    comm.ExecuteNon Query();

    }

  • Cor Ligthert [MVP]

    #2
    Re: Problem with AddWithValue

    Slimshim,

    If I see it well, than you only need to set the value.
    The AddWithValue is to add a parameteritem to the parametercollec tion with a
    value.
    (and than use an indexer to tell which parameter from the collection).

    http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

    Now you are in my idea trying to do that 4 times.

    Cor

    "SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
    news:686CD70C-6198-4D0E-BD61-3855B7009992@mi crosoft.com...
    Hi,
    I"m trying to insert into a database with no adapter .(AddWithValue) I get
    an error when
    executing ExcecuteNonQuer y . Parameter pName has no default value. what am
    I
    doing wrong ?
    >
    using (OleDbConnectio n conn = new OleDbConnection (
    >
    testacc.Propert ies.Settings.De fault.mytestCon nectionString))
    {
    >
    using (OleDbCommand comm = new OleDbCommand(
    "INSERT INTO phonebook (name, phonenum) VALUES
    (pName, pPhonenum)", conn))
    {
    comm.Parameters .Add("pName",
    OleDbType.VarCh ar).SourceColum n = "name";
    >
    comm.Parameters .Add("pPhonenum ",
    OleDbType.VarCh ar).SourceColum n = "phonenum";
    comm.Parameters .AddWithValue(" pName", "John Doe");
    comm.Parameters .AddWithValue(" pPhonenum",
    "555-555-1212");
    conn.Open();
    comm.ExecuteNon Query();
    >
    }
    >

    Comment

    • SLIMSHIM

      #3
      Re: Problem with AddWithValue

      I appreciate your help.
      I still don't understand. Do I not use the .Add to set up the fields ?
      Or am I missing a third step ?

      I appoligize for being confused ...

      "Cor Ligthert [MVP]" wrote:
      Slimshim,
      >
      If I see it well, than you only need to set the value.
      The AddWithValue is to add a parameteritem to the parametercollec tion with a
      value.
      (and than use an indexer to tell which parameter from the collection).
      >
      http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx
      >
      Now you are in my idea trying to do that 4 times.
      >
      Cor
      >
      "SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
      news:686CD70C-6198-4D0E-BD61-3855B7009992@mi crosoft.com...
      Hi,
      I"m trying to insert into a database with no adapter .(AddWithValue) I get
      an error when
      executing ExcecuteNonQuer y . Parameter pName has no default value. what am
      I
      doing wrong ?

      using (OleDbConnectio n conn = new OleDbConnection (

      testacc.Propert ies.Settings.De fault.mytestCon nectionString))
      {

      using (OleDbCommand comm = new OleDbCommand(
      "INSERT INTO phonebook (name, phonenum) VALUES
      (pName, pPhonenum)", conn))
      {
      comm.Parameters .Add("pName",
      OleDbType.VarCh ar).SourceColum n = "name";

      comm.Parameters .Add("pPhonenum ",
      OleDbType.VarCh ar).SourceColum n = "phonenum";
      comm.Parameters .AddWithValue(" pName", "John Doe");
      comm.Parameters .AddWithValue(" pPhonenum",
      "555-555-1212");
      conn.Open();
      comm.ExecuteNon Query();

      }
      >
      >
      >

      Comment

      • SLIMSHIM

        #4
        Re: Problem with AddWithValue

        I figured out how it's done.
        string tname = "simon";
        string tphonenum = "555-555-1212";

        comm.Parameters .Add("pName", OleDbType.VarCh ar, tname.Length,"n ame").Value =
        tname;

        comm.Parameters .Add("pPhonenum ",
        OleDbType.VarCh ar,tphonenum.Le ngth,"phonenum" ).Value = tphonenum;
        this will do the trick.

        "SLIMSHIM" wrote:
        I appreciate your help.
        I still don't understand. Do I not use the .Add to set up the fields ?
        Or am I missing a third step ?
        >
        I appoligize for being confused ...
        >
        "Cor Ligthert [MVP]" wrote:
        >
        Slimshim,

        If I see it well, than you only need to set the value.
        The AddWithValue is to add a parameteritem to the parametercollec tion with a
        value.
        (and than use an indexer to tell which parameter from the collection).

        http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

        Now you are in my idea trying to do that 4 times.

        Cor

        "SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
        news:686CD70C-6198-4D0E-BD61-3855B7009992@mi crosoft.com...
        Hi,
        I"m trying to insert into a database with no adapter .(AddWithValue) I get
        an error when
        executing ExcecuteNonQuer y . Parameter pName has no default value. what am
        I
        doing wrong ?
        >
        using (OleDbConnectio n conn = new OleDbConnection (
        >
        testacc.Propert ies.Settings.De fault.mytestCon nectionString))
        {
        >
        using (OleDbCommand comm = new OleDbCommand(
        "INSERT INTO phonebook (name, phonenum) VALUES
        (pName, pPhonenum)", conn))
        {
        comm.Parameters .Add("pName",
        OleDbType.VarCh ar).SourceColum n = "name";
        >
        comm.Parameters .Add("pPhonenum ",
        OleDbType.VarCh ar).SourceColum n = "phonenum";
        comm.Parameters .AddWithValue(" pName", "John Doe");
        comm.Parameters .AddWithValue(" pPhonenum",
        "555-555-1212");
        conn.Open();
        comm.ExecuteNon Query();
        >
        }
        >

        Comment

        • Dave Sexton

          #5
          Re: Problem with AddWithValue

          Hi,

          The "Add" method adds the parameter and the "AddWithVal ue" method adds the
          parameter along with its value, simultaneously. You don't need to use both
          "Add" and "AddWithVal ue". Use one or the other for each parameter.

          comm.Parameters .AddWithValue(" pName", tname);
          comm.Parameters .AddWithValue(" phonenum", tphonenum);

          --
          Dave Sexton

          "SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comwrote in message
          news:46AC493B-6399-46FE-8C82-92A84E6F8EE5@mi crosoft.com...
          >I figured out how it's done.
          string tname = "simon";
          string tphonenum = "555-555-1212";
          >
          comm.Parameters .Add("pName", OleDbType.VarCh ar, tname.Length,"n ame").Value =
          tname;
          >
          comm.Parameters .Add("pPhonenum ",
          OleDbType.VarCh ar,tphonenum.Le ngth,"phonenum" ).Value = tphonenum;
          this will do the trick.
          >
          "SLIMSHIM" wrote:
          >
          >I appreciate your help.
          >I still don't understand. Do I not use the .Add to set up the fields ?
          >Or am I missing a third step ?
          >>
          >I appoligize for being confused ...
          >>
          >"Cor Ligthert [MVP]" wrote:
          >>
          Slimshim,
          >
          If I see it well, than you only need to set the value.
          The AddWithValue is to add a parameteritem to the parametercollec tion
          with a
          value.
          (and than use an indexer to tell which parameter from the collection).
          >
          http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx
          >
          Now you are in my idea trying to do that 4 times.
          >
          Cor
          >
          "SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
          news:686CD70C-6198-4D0E-BD61-3855B7009992@mi crosoft.com...
          Hi,
          I"m trying to insert into a database with no adapter .(AddWithValue) I
          get
          an error when
          executing ExcecuteNonQuer y . Parameter pName has no default value. what
          am
          I
          doing wrong ?
          >
          using (OleDbConnectio n conn = new OleDbConnection (
          >
          testacc.Propert ies.Settings.De fault.mytestCon nectionString))
          {
          >
          using (OleDbCommand comm = new OleDbCommand(
          "INSERT INTO phonebook (name, phonenum) VALUES
          (pName, pPhonenum)", conn))
          {
          comm.Parameters .Add("pName",
          OleDbType.VarCh ar).SourceColum n = "name";
          >
          comm.Parameters .Add("pPhonenum ",
          OleDbType.VarCh ar).SourceColum n = "phonenum";
          comm.Parameters .AddWithValue(" pName", "John Doe");
          comm.Parameters .AddWithValue(" pPhonenum",
          "555-555-1212");
          conn.Open();
          comm.ExecuteNon Query();
          >
          }
          >
          >
          >
          >

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Problem with AddWithValue

            Hi,

            The OleDB documentation on MSDN and the notations itself areconfusing.

            comm.Parameters .Add("?", OleDbType.VarCh ar);
            comm.Parameters[0]].Value = tphonenum;

            is enough,

            As Dave wrote you can do it as well with the AddWithValue which replaces the
            overloaded Add from 1.x in version 2.0. The caveat at that, is that you can
            only do it once (because it is a collection), which is seldom what is
            wanted.

            Be aware that this is fonfusing because this is where SQLClient and OleDb
            have different notations but on many places is in MSDN taken the SQLClient
            notation at OleDb. Which does not go wrong by the way, but OleDb has AFAIK
            no named parameters which are working. (You can type everything there).

            I hope this gives some ideas

            Cor


            comm.Parameters .Add("pPhonenum ",
            OleDbType.VarCh ar,tphonenum.Le ngth,"phonenum" ).Value = tphonenum;


            "SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
            news:46AC493B-6399-46FE-8C82-92A84E6F8EE5@mi crosoft.com...
            >I figured out how it's done.
            string tname = "simon";
            string tphonenum = "555-555-1212";
            >
            comm.Parameters .Add("pName", OleDbType.VarCh ar, tname.Length,"n ame").Value
            =
            tname;
            >
            comm.Parameters .Add("pPhonenum ",
            OleDbType.VarCh ar,tphonenum.Le ngth,"phonenum" ).Value = tphonenum;
            this will do the trick.
            >
            "SLIMSHIM" wrote:
            >
            >I appreciate your help.
            >I still don't understand. Do I not use the .Add to set up the fields ?
            >Or am I missing a third step ?
            >>
            >I appoligize for being confused ...
            >>
            >"Cor Ligthert [MVP]" wrote:
            >>
            Slimshim,
            >
            If I see it well, than you only need to set the value.
            The AddWithValue is to add a parameteritem to the parametercollec tion
            with a
            value.
            (and than use an indexer to tell which parameter from the collection).
            >
            http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx
            >
            Now you are in my idea trying to do that 4 times.
            >
            Cor
            >
            "SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
            news:686CD70C-6198-4D0E-BD61-3855B7009992@mi crosoft.com...
            Hi,
            I"m trying to insert into a database with no adapter .(AddWithValue)
            I get
            an error when
            executing ExcecuteNonQuer y . Parameter pName has no default value.
            what am
            I
            doing wrong ?
            >
            using (OleDbConnectio n conn = new OleDbConnection (
            >
            testacc.Propert ies.Settings.De fault.mytestCon nectionString))
            {
            >
            using (OleDbCommand comm = new OleDbCommand(
            "INSERT INTO phonebook (name, phonenum) VALUES
            (pName, pPhonenum)", conn))
            {
            comm.Parameters .Add("pName",
            OleDbType.VarCh ar).SourceColum n = "name";
            >
            comm.Parameters .Add("pPhonenum ",
            OleDbType.VarCh ar).SourceColum n = "phonenum";
            comm.Parameters .AddWithValue(" pName", "John
            Doe");
            comm.Parameters .AddWithValue(" pPhonenum",
            "555-555-1212");
            conn.Open();
            comm.ExecuteNon Query();
            >
            }
            >
            >
            >
            >

            Comment

            • Rad

              #7
              Re: Problem with AddWithValue

              Also you need to be clear which provider you are using. Some
              providers, e.g. Sybase ASA will throw an exception if you use named
              parameters.

              On Mon, 13 Nov 2006 05:42:30 +0100, "Cor Ligthert [MVP]"
              <notmyfirstname @planet.nlwrote :
              >Hi,
              >
              >The OleDB documentation on MSDN and the notations itself areconfusing.
              >
              >comm.Parameter s.Add("?", OleDbType.VarCh ar);
              >comm.Parameter s[0]].Value = tphonenum;
              >
              >is enough,
              >
              >As Dave wrote you can do it as well with the AddWithValue which replaces the
              >overloaded Add from 1.x in version 2.0. The caveat at that, is that you can
              >only do it once (because it is a collection), which is seldom what is
              >wanted.
              >
              >Be aware that this is fonfusing because this is where SQLClient and OleDb
              >have different notations but on many places is in MSDN taken the SQLClient
              >notation at OleDb. Which does not go wrong by the way, but OleDb has AFAIK
              >no named parameters which are working. (You can type everything there).
              >
              >I hope this gives some ideas
              >
              >Cor
              >
              >
              >comm.Parameter s.Add("pPhonenu m",
              >OleDbType.VarC har,tphonenum.L ength,"phonenum ").Value = tphonenum;
              >
              >
              >"SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
              >news:46AC493 B-6399-46FE-8C82-92A84E6F8EE5@mi crosoft.com...
              >>I figured out how it's done.
              >string tname = "simon";
              >string tphonenum = "555-555-1212";
              >>
              >comm.Parameter s.Add("pName", OleDbType.VarCh ar, tname.Length,"n ame").Value
              >=
              >tname;
              >>
              >comm.Parameter s.Add("pPhonenu m",
              >OleDbType.VarC har,tphonenum.L ength,"phonenum ").Value = tphonenum;
              >this will do the trick.
              >>
              >"SLIMSHIM" wrote:
              >>
              >>I appreciate your help.
              >>I still don't understand. Do I not use the .Add to set up the fields ?
              >>Or am I missing a third step ?
              >>>
              >>I appoligize for being confused ...
              >>>
              >>"Cor Ligthert [MVP]" wrote:
              >>>
              >Slimshim,
              >>
              >If I see it well, than you only need to set the value.
              >The AddWithValue is to add a parameteritem to the parametercollec tion
              >with a
              >value.
              >(and than use an indexer to tell which parameter from the collection).
              >>
              >http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx
              >>
              >Now you are in my idea trying to do that 4 times.
              >>
              >Cor
              >>
              >"SLIMSHIM" <SLIMSHIM@discu ssions.microsof t.comschreef in bericht
              >news:686CD70 C-6198-4D0E-BD61-3855B7009992@mi crosoft.com...
              >Hi,
              >I"m trying to insert into a database with no adapter .(AddWithValue)
              >I get
              >an error when
              >executing ExcecuteNonQuer y . Parameter pName has no default value.
              >what am
              >I
              >doing wrong ?
              >>
              >using (OleDbConnectio n conn = new OleDbConnection (
              >>
              >testacc.Proper ties.Settings.D efault.mytestCo nnectionString) )
              > {
              >>
              > using (OleDbCommand comm = new OleDbCommand(
              > "INSERT INTO phonebook (name, phonenum) VALUES
              >(pName, pPhonenum)", conn))
              > {
              > comm.Parameters .Add("pName",
              >OleDbType.VarC har).SourceColu mn = "name";
              >>
              > comm.Parameters .Add("pPhonenum ",
              >OleDbType.VarC har).SourceColu mn = "phonenum";
              > comm.Parameters .AddWithValue(" pName", "John
              >Doe");
              > comm.Parameters .AddWithValue(" pPhonenum",
              >"555-555-1212");
              > conn.Open();
              > comm.ExecuteNon Query();
              >>
              > }
              >>
              >>
              >>
              >>
              >

              Comment

              Working...