none
error:No value given for one or more required parameters.

    Question

  • I am using C#.net to write a program. I have an update to update the database when the user changes the cooment. All values that I have assign to the datarow as data in it. Here is the code:

    foreach (DataRow dr in _dsComment.Tables[0].Rows)

    {

    if (dr["CommentId"].ToString() == strId)

    {

    dr["OrdNo"] = _Order;

    dr["Comment"] = frmN.Comment;

    dr["Date"] = System.DateTime.Now.ToShortDateString();

    dr["Name"] = UserName;

    }

    }

     

    _DaComment.Update(_dsComment, "Comment");

    _dsComment.AcceptChanges();

    the Error I am getting is: No value given for one or more required parameters.
    this is throw at the update.

    my update statement is:

    _DaComment.UpdateCommand = con.CreateCommand();

    _DaComment.UpdateCommand.CommandText =

    "UPDATE Comment " +

    "SET Name = @Name, Comment = @Comment, [Date] = @Date, OrdNo = @OrdNo " +

    "WHERE CommmentId = @CommentId";



    What is the Problem?????????????????

    Friday, November 18, 2005 4:22 PM

Answers

  • The problem is that you need to associate parameters with the update command:

    _DaComment.UpdateCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 15, "Name");

    Naturally, the second and third parameters will vary depending on the datatype in the database.

    Hope that helps.

    Friday, November 18, 2005 8:41 PM
    Moderator

All replies

  • The problem is that you need to associate parameters with the update command:

    _DaComment.UpdateCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 15, "Name");

    Naturally, the second and third parameters will vary depending on the datatype in the database.

    Hope that helps.

    Friday, November 18, 2005 8:41 PM
    Moderator
  • Here is all of my update command. I have what you said. I use this update to add a new record and it works fine, but when I edit a record it throws the error


    _DaComment.UpdateCommand = con.CreateCommand();

    _DaComment.UpdateCommand.CommandText =

    "UPDATE Comment " +

    "SET OrdNo = @OrdNo, Name = @Name, Comment = @Comment, [Date] = @Date " +

    "WHERE CommmentId = @CommentId";

    // Bind parameters to appropriate columns for UPDATE command

    this.AddParms(_DaComment.UpdateCommand,"OrdNo", "Name", "Comment", "Date", "CommentId");

    Tuesday, November 22, 2005 2:18 PM
  • And AddParms is what? I don't recognize the method as something intrinsic to .NET.

    If this is something that you've written, check to see if you are including the '@' in the name of the parameters. That is critical to getting the UpdateCommand (or any other Command class) to work.

    Tuesday, November 22, 2005 2:49 PM
    Moderator
  • Here is the code for addparms

    private
    void AddParms(OleDbCommand cmd, params string[] cols)

    {

    // Add each parameter. Note that each colum in

    // table "Customers" is of type VARCHAR !

    foreach (String column in cols)

    {

    cmd.Parameters.Add(

    "@" + column, OleDbType.Char, 0, column);

    }

    }

    Tuesday, November 22, 2005 2:51 PM
  • Aha!!!

    The problem is that the OleDbCommand class doesn't support passing named parameters when the CommandType is set to Text (which is the default). It only supports positional parameters.

    Tuesday, November 22, 2005 3:05 PM
    Moderator
  • the update works in other areas but it does not work when the comment is chaged.
    Thursday, December 01, 2005 3:48 PM