Domanda

get this error to the txtNum.Text i dont know what to do i already tried the

Convert.ToInt32 and SqlDbType.Int but it doesnt work please help me

the total has a $ sign in it maybe that's the problem. the Total's DataType is Decimal

com = new SqlCommand(@"INSERT INTO Orders(
                        OrderDate, 
                        Username, 
                        FirstName, 
                        LastName, 
                        Address, 
                        Phone, 
                        Total, 
                        HasBeenShipped
                    ) 
                    VALUES(
                        GetDate(),
                        @p5, 
                        @p1, 
                        @p2, 
                        @p3, 
                        @p4, 
                        @p6,
                        'false'
                    )", con2);
com.Parameters.AddWithValue("@p5", Label2.Text);
com.Parameters.AddWithValue("@p1", txtFname.Text);
com.Parameters.AddWithValue("@p2", TxtLname.Text);
com.Parameters.AddWithValue("@p3", TxtAdd.Text);
com.Parameters.AddWithValue("@p4", Convert.ToInt32(TxtNum.Text));
com.Parameters.AddWithValue("@p6", lblTotal.Text);
com.ExecuteNonQuery();
È stato utile?

Soluzione

If lblTotal is formatted as a currency then you can use:

com.Parameters.AddWithValue("@p6", Decimal.Parse(lblTotal.Text,NumberStyles.Currency));

Obviously it will throw an exception if it's not a valid currency value - you'd need to decide what to do in that case.

Altri suggerimenti

I suppose that the last field HasBeenShipped is a bit datatype in the database.
If this is the case then pass 0 (the numeric value) for it not 'false' (a string)

Moreover it is the parameter @p6 that contains the value for the field Total and thus it is this parameter that should be converted to a number, while the parameter @p4 could be a string if the field Phone is a NVarChar

com = new SqlCommand(@"INSERT INTO Orders 
             (OrderDate, Username, FirstName, LastName, Address, Phone, Total, HasBeenShipped)
       VALUES(GetDate(), @p5,      @p1,       @p2,      @p3,     @p4,   @p6,   0)", con2);
com.Parameters.AddWithValue("@p5", Label2.Text);
com.Parameters.AddWithValue("@p1", txtFname.Text);
com.Parameters.AddWithValue("@p2", TxtLname.Text);
com.Parameters.AddWithValue("@p3", TxtAdd.Text);
com.Parameters.AddWithValue("@p4", TxtNum.Text);
com.Parameters.AddWithValue("@p6", Convert.ToInt32(lblTotal.Text));

EDIT Following your comment below, if the lblTotal.Text contains the currency symbol then you cannot convert directly to an integer with Convert.ToInt32, you should use decimal.TryParse

decimal totalValue
decimal.TryParse(lblTotal.Text, NumberStyles.Currency, 
                 CultureInfo.CurrentCulture, out totalValue);
com.Parameters.AddWithValue("@p6", totalValue);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top