简体   繁体   中英

specified cast is not valid. in c# window web application

I wish to create an invoice window form application, I get the error when I want to store the data that I have select into the database.The invoice ID should be auto generated, but now I manual insert invoiceID=6; to test whether the data can load into the database. However I get the error when I execute the reader. My overview of data table can refer at here

Properties of invoiceID Column

  1. Data Type: int;
  2. Identity Increment:0;
  3. Identity Seed:0;
  4. Is Identity:False;
  5. Length:4;
  6. Nullable: False;
  7. Precision:10;
  8. Scale:0

Error:

Specified cast is not valid

Edited Code:

private void StoreData()
    {
        int invoiceID;
        int Item_Id;
        int quantity;

        using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\xchoo\Documents\Visual Studio 2012\Projects\WindowsFormsApplication_test2\WindowsFormsApplication_test2\Data.mdf;Integrated Security=True;Connect Timeout=30"))
        {
            con.Open();


            using (var cmd = con.CreateCommand())
            {
                cmd.CommandText = @"insert into Invoice(invoiceID, subtotal,tax,total) values (7, @subtotal,@tax,@total); select SCOPE_IDENTITY() as invoiceID;";
                cmd.Parameters.AddWithValue("@subtotal", subtotal);
                cmd.Parameters.AddWithValue("@tax", tax);
                cmd.Parameters.AddWithValue("@total", total);
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())                       
                     {
                         //invoiceID = cmd.GetInt32("InvoiceID");
                        //invoiceID = (int)reader["invoiceID"];  //error
                         cmd.Parameters.AddWithValue("@invoiceID", Convert.ToInt32("invoiceID"));
                    }

                }
            }
            foreach (var item in OrderItems.Rows)
            {
                using (var cmd = con.CreateCommand())
                {

                    cmd.CommandText = @"insert into InvoiceItem(invoiceID,Item_Id,quantity) values (@invoiceID,@Item_Id,@quantity);";
                     cmd.Parameters.AddWithValue("@InvoiceID", invoiceID);
                     cmd.Parameters.AddWithValue("@Item_Id", Item_Id);
                     cmd.Parameters.AddWithValue("@quantity", quantity);
                   // cmd.Parameters.AddWithValue("@invoiceID", Convert.ToInt32("invoiceID"));
                    //cmd.Parameters.AddWithValue("@Item_Id", Convert.ToInt32("Item_Id"));
                    //cmd.Parameters.AddWithValue("@quantity", Convert.ToInt32("quantity"));
                    cmd.ExecuteNonQuery();



                }
            }

            con.Close();
        }
    }

1 . line cmd.CommandText = @"insert into Invoice(invoiceID, subtotal,tax,total) values (6, @subtotal,@tax,@total); select SCOPE_IDENTITY() as invoiceID;";

invoiceID is not not Identity column in Invoice

 select SCOPE_IDENTITY() as invoiceID

will return null after insert

also instead of

using (var reader = cmd.ExecuteReader())            
{
     if (reader.Read())                       
     {
         cmd.Parameters.AddWithValue("@invoiceID", Convert.ToInt32("invoiceID"));
     }
}

i would try the following code to get result

object idObj = cmd.ExecuteScalar()

2 . line cmd.Parameters.AddWithValue("@invoiceID", Convert.ToInt32("invoiceID"));

Convert.ToInt32("invoiceID") will never work becuase "invoiceID" is not a number

remove quotes: Convert.ToInt32(invoiceID)

the same problem with Convert.ToInt32("Item_Id") and Convert.ToInt32("quantity")

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