locked
No value given for one or more required parameters RRS feed

  • Question

  •  

    I am not sure why I am getting no value given for one or more required parameters.

    I am saving to two tables, the first table saves Ok. I have Students_ID on both table.

    if showing the second table will help, I will go ahead and paste it

    cmd1.Connection = conn cmd1.CommandText = "INSERT INTO StudentNOK(Student_ID, CountryNK,GenderNK, PhoneNK, AltPhoneNK, RelationshipNK,EmailNK)VALUES( @StuID,@SNameNk,@FNameNk,@MidNamNK" & _ ", @AddrNk, @cityNK, @LGNK, @StateNk, @CntryNk, @GenderNK, @PhoNK, @AltPhoNK, @CmbRel,@EmailNK)" cmd.Parameters.AddWithValue("@StuID", txtStudentID.Text) cmd.Parameters.AddWithValue("@CountryNK", txtCountryNK.Text) cmd.Parameters.AddWithValue("@GenderNK", CmbGenderNk.Text) cmd.Parameters.AddWithValue("@phNk", txtPhoneNK.Text) cmd.Parameters.AddWithValue("@AltPhNK", txtAltPhoneNK.Text) cmd.Parameters.AddWithValue("@RelNK", cmbRelation.Text) cmd.Parameters.AddWithValue("@EmailNk", txtEmail.Text) cmd1.ExecuteNonQuery() conn.Close() cmd1.Dispose()


    Saturday, August 19, 2017 3:40 PM

Answers

  • I have cleaned up and narrowed my code down to these Items to make it more readable. I regret the inconvenience. However, I am still getting the same error message "No value given for one or more required parameters." As I mentioned I am using Student_ID in the "Student" table and also on this second table "StudentNOK" I am not sure if this could be contributing to the problem but I thought it is worth mentioning. thank you for your time

     cmd1.Connection = conn

    cmd1.CommandText = "INSERT INTO StudentNOK" & _ "

    (Student_ID, SurNameNK, FirstNameNK)VALUES( @StuID,@SNameNk,@FNameNK)" cmd.Parameters.AddWithValue("@StuID", txtStudentID.Text)

    cmd.Parameters.AddWithValue("@SNameNK", txtSurnameNK.Text)

    cmd.Parameters.AddWithValue("@FNameNK", txtFirstNameNK.Text)

    cmd1.ExecuteNonQuery() conn.Close()

    cmd1.Dispose()



    • Edited by alobi Sunday, August 20, 2017 5:35 PM
    • Marked as answer by alobi Sunday, August 20, 2017 6:09 PM
    Sunday, August 20, 2017 5:26 PM

All replies

  • Try this command:

    cmd1.CommandText = "INSERT INTO StudentNOK " & _
                       "       ( Student_ID, CountryNK,  GenderNK,  PhoneNK, AltPhoneNK, RelationshipNK, EmailNK) " & _
                       "VALUES ( @StuID,     @CountryNk, @GenderNK, @phNk,   @AltPhNK,   @RelNK,         @EmailNk)"
    
    Re-check the names.

    Saturday, August 19, 2017 6:52 PM
  • Hello,

    If you simply look at the number of fields in the first part then the count of fields in the value section they are unequal along with how many values are done with AddWithValue, this is why there is an issue, one table 

    INSERT INTO StudentNOK(
    	Student_ID, 
    	CountryNK,
    	GenderNK, 
    	PhoneNK, 
    	AltPhoneNK, 
    	RelationshipNK,
    	EmailNK)
    VALUES( 
    	@StuID,
    	@SNameNk,
    	@FNameNk,
    	@MidNamNK
        @AddrNk, 
    	@cityNK, 
    	@LGNK, 
    	@StateNk, 
    	@CntryNk, 
    	@GenderNK,  
    	@PhoNK, 
    	@AltPhoNK, 
    	@CmbRel,
    	@EmailNK)
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, August 19, 2017 9:04 PM
  • I have cleaned up and narrowed my code down to these Items to make it more readable. I regret the inconvenience. However, I am still getting the same error message "No value given for one or more required parameters." As I mentioned I am using Student_ID in the "Student" table and also on this second table "StudentNOK" I am not sure if this could be contributing to the problem but I thought it is worth mentioning. thank you for your time

     cmd1.Connection = conn

    cmd1.CommandText = "INSERT INTO StudentNOK" & _ "

    (Student_ID, SurNameNK, FirstNameNK)VALUES( @StuID,@SNameNk,@FNameNK)" cmd.Parameters.AddWithValue("@StuID", txtStudentID.Text)

    cmd.Parameters.AddWithValue("@SNameNK", txtSurnameNK.Text)

    cmd.Parameters.AddWithValue("@FNameNK", txtFirstNameNK.Text)

    cmd1.ExecuteNonQuery() conn.Close()

    cmd1.Dispose()



    • Edited by alobi Sunday, August 20, 2017 5:35 PM
    • Marked as answer by alobi Sunday, August 20, 2017 6:09 PM
    Sunday, August 20, 2017 5:26 PM
  • Use cmd1 instead of cmd.

    • Proposed as answer by Cor Ligthert Sunday, August 20, 2017 6:05 PM
    Sunday, August 20, 2017 5:38 PM
  • Awesome! Thanks
    Sunday, August 20, 2017 6:10 PM