简体   繁体   中英

System.FormatException: 'Failed to convert parameter value from a String to a Int32.'

I keep getting the error in the title when i am trying to pass data to my database using a c# windows form.

For reference, I am trying to use the Scope_Identity to get the current PK from one table and then pass it to another table as a FK . But i am having trouble making this work. I have no doubt I am missing something or doing something wrong, but I honestly cannot find a solution to what I need. Here is the code I am using.

SqlConnection con = new SqlConnection(@"");
        con.Open();
        {
            using (SqlCommand cmd = new SqlCommand("INSERT dbo.Booking(CustomerID,VehicleID,DateofBooking," +
                "DurationBooked, ExpectedReturnDate) VALUES( @CustID, @VehicleID, @DOBooking, @DurationBooked,@ExpectedReturn); INSERT dbo.Payments (BookingID,PaymentMethod,Discount,PaymentAmount," +
                "PaymentDate,PaymentReceived) VALUES( SCOPE_IDENTITY(),@PayMeth,@PayAmount,@PayDate, @PayReceived, @Discount)"))

            {
                cmd.Connection = con;
                cmd.Parameters.Add("@CustID", SqlDbType.Int).Value = comboBox1.ValueMember;
                cmd.Parameters.Add("@VehicleID", SqlDbType.Int).Value = comboBox2.ValueMember;
                cmd.Parameters.Add("@DOBooking", SqlDbType.Date).Value = datefrom.Text;
                cmd.Parameters.Add("@ExpectedReturn", SqlDbType.Date).Value = dateTo.Text;
                cmd.Parameters.Add("@DurationBooked", SqlDbType.Float, 3).Value = Duration.Text;
                cmd.Parameters.Add("@PayMeth", SqlDbType.VarChar, 15).Value = listBox1.Text;
                cmd.Parameters.Add("@PayAmount", SqlDbType.Money).Value = PayAmount.Text;
                cmd.Parameters.Add("@PayDate", SqlDbType.Date).Value = DatePay.Text;
                cmd.Parameters.Add("@PayReceived", SqlDbType.VarChar, 3).Value = PayReceived.Text;
                cmd.Parameters.Add("@Discount", SqlDbType.SmallMoney).Value = Discount.Text;

            }

I am having a ridiculous amount of hassle trying to Pass this PK to another Table as a FK For reference, it is the BookingID from the Booking table that i am trying to pass to the Payments table in the BookingID column which is a FK relationship

Error Image

I think you're trying to fight with a different problem. you have conversion issue, but not PK or FK key consistency.

please check if all the params they you send have the same type as described in your instruction.

It can be also that even your scope_identity() has type different from value. I don't know if you covered this with Keys constraints

by the way. out of scope. There is also a way to get last id is to use @@IDENTITY.

The @@identity function returns the last identity created in the same session. The scope_identity() function returns the last identity created in the same session and the same scope.

FIXED. Thank you to all those who helped. The issue was indeed with my Parameters, specifically with dates. I needed to change the DataType in SQL server to DateTime , as well as ensuring the query would run in SQL Server first helped.

 using (SqlCommand cmd = new SqlCommand("INSERT dbo.Booking(CustomerID,VehicleID,DateofBooking, DurationBooked, ExpectedReturnDate) " +
                "VALUES( @CustID, @VehicleID, @DOBooking, @DurationBooked,@ExpectedReturn) SELECT SCOPE_IDENTITY(); " +
                "INSERT dbo.Payments (BookingID,PaymentMethod,Discount,PaymentAmount, PaymentDate,PaymentReceived)" +
                "VALUES( SCOPE_IDENTITY(),@PayMeth,@Discount,@PayAmount, @PayDate, @PayReceived)"))

            {
                cmd.Connection = con;
                cmd.Parameters.Add("@CustID", SqlDbType.Int).Value = Convert.ToInt32(CustID.SelectedValue);
                cmd.Parameters.Add("@VehicleID", SqlDbType.Int).Value = Convert.ToInt32(VehicleID.SelectedValue);
                cmd.Parameters.Add("@DOBooking", SqlDbType.DateTime).Value = DateofBooking.Value.Date;
                cmd.Parameters.Add("@ExpectedReturn", SqlDbType.DateTime).Value = ExpectedReturnDate.Value.Date;
                cmd.Parameters.Add("@DurationBooked", SqlDbType.Float, 3).Value = Duration.Text;
                cmd.Parameters.Add("@PayMeth", SqlDbType.VarChar, 15).Value = PaymentMethod.Text;
                cmd.Parameters.Add("@PayAmount", SqlDbType.Money).Value = PayAmount.Text;
                cmd.Parameters.Add("@PayDate", SqlDbType.DateTime).Value = PayDate.Value.Date;
                cmd.Parameters.Add("@PayReceived", SqlDbType.VarChar, 3).Value = Received.Text;
                cmd.Parameters.Add("@Discount", SqlDbType.VarChar, 3).Value = Discount.Text;
                if (cmd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("Record inserted");
                }
                else
                {
                    MessageBox.Show("Customer failed");
                }

For Ref, Scope_Identity worked perfectly.

Thanks for all your support, I know this may seem basic for some of you, but i am new to all this and trying to self teach, so as you can imagine.. it was a relief even this little issue is solved. Thanks guys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM