locked
i trying to pass some parameters through C# code but i need to 4 into one RRS feed

  • Question

  • User32275318 posted

    here is my code: there i need to add 3 dropdownlists for date of birth. i wasnt able to figure out how to pass those parameters


                SqlCommand cmd = new SqlCommand("insert into reg" + "(fname,mname,lname,Lid,pswd,rpswd,gender,dob,rmid,mobile(@fname,@mname,@lname,@Lid,@pswd,@rpswd,@gender,@dob,@rmid,@mobile)",con);
                cmd.Parameters.AddWithValue("@fname", TextBox1.Text);
                cmd.Parameters.AddWithValue("@mname", TextBox2.Text);
                cmd.Parameters.AddWithValue("@lname", TextBox3.Text);
                cmd.Parameters.AddWithValue("@Lid", TextBox4.Text);
                cmd.Parameters.AddWithValue("@pswd", TextBox5.Text);
                cmd.Parameters.AddWithValue("@rpswd", TextBox6.Text);
                cmd.Parameters.AddWithValue("@gender", DropDownList4.SelectedValue);
               

                cmd.Parameters.AddWithValue("@dob", DropDownList1.SelectedValue, DropDownList2.SelectedValue, DropDownList3.SelectedValue);
              

                cmd.Parameters.AddWithValue("@rmid", TextBox7.Text);
                cmd.Parameters.AddWithValue("@mobile", TextBox8.Text);
                cmd.ExecuteNonQuery();
                Button1.Text = "Register insert";

    Wednesday, March 15, 2017 6:18 PM

Answers

  • User2117486576 posted

    Do you want to insert three rows - one for each of the dates of birth?

    If you are using Sql Server, then the only way is to either use a stored procedure and send the multiple dates of birth as an array or Table Valued Parameter; OR, append all three insert statements to your SqlCommand.  You can send multiple commands as an anonymous block.  Simply start the command test with a "Begin" and terminate it with an "End".  This is a pretty ugly solution and I would suggest the stored procedure approach.

    If you are using Oracle, then you can tell the ODP.Net command the number of rows to insert by setting the ArrayBindCount property to the number of dates of birth and setting the value of the DOB parameter to an array of the dates.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 15, 2017 6:48 PM
  • User753101303 posted

    Hi,

    Assuming your drop downs are exposing day, month and year numbers you could use new DateTime(year,month,day) to create a date value and pass to the @dob parameter. It could look like (not sure in  which order they are) :

     cmd.Parameters.AddWithValue("@dob", new DateTime(int.Parse(DropDownList3.SelectedValue), int.Parse(DropDownList2.SelectedValue), int.Parse(DropDownList1.SelectedValue));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 15, 2017 6:49 PM
  • User-2057865890 posted

    Hi Ram@123,

    System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

    The documentation is pretty clear on what the allowed values are.

    Exception Condition
    ArgumentOutOfRangeException

    year is less than 1 or greater than 9999.

    -or-

    month is less than 1 or greater than 12.

    -or-

    day is less than 1 or greater than the number of days in month.

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 21, 2017 7:30 AM

All replies

  • User2117486576 posted

    Do you want to insert three rows - one for each of the dates of birth?

    If you are using Sql Server, then the only way is to either use a stored procedure and send the multiple dates of birth as an array or Table Valued Parameter; OR, append all three insert statements to your SqlCommand.  You can send multiple commands as an anonymous block.  Simply start the command test with a "Begin" and terminate it with an "End".  This is a pretty ugly solution and I would suggest the stored procedure approach.

    If you are using Oracle, then you can tell the ODP.Net command the number of rows to insert by setting the ArrayBindCount property to the number of dates of birth and setting the value of the DOB parameter to an array of the dates.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 15, 2017 6:48 PM
  • User753101303 posted

    Hi,

    Assuming your drop downs are exposing day, month and year numbers you could use new DateTime(year,month,day) to create a date value and pass to the @dob parameter. It could look like (not sure in  which order they are) :

     cmd.Parameters.AddWithValue("@dob", new DateTime(int.Parse(DropDownList3.SelectedValue), int.Parse(DropDownList2.SelectedValue), int.Parse(DropDownList1.SelectedValue));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 15, 2017 6:49 PM
  • User32275318 posted

    i am not using stored procedure

    Wednesday, March 15, 2017 7:16 PM
  • User32275318 posted

    System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

    it gives the following error when i try give some values into sql

    Friday, March 17, 2017 4:13 PM
  • User753101303 posted

    And you checked which values are used ? As you are using drop downs, do you handle the case where someone select "31" in the day dropdown for a month that doesn't have 31 days ???

    Friday, March 17, 2017 5:06 PM
  • User32275318 posted

    no i haven't checked with that

    Friday, March 17, 2017 6:46 PM
  • User-2057865890 posted

    Hi Ram@123,

    System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

    The documentation is pretty clear on what the allowed values are.

    Exception Condition
    ArgumentOutOfRangeException

    year is less than 1 or greater than 9999.

    -or-

    month is less than 1 or greater than 12.

    -or-

    day is less than 1 or greater than the number of days in month.

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 21, 2017 7:30 AM