1

I'm trying to update my table with this LINQ query

public void updateProduct(
    int selectedIDToUpdate, 
    string prodNAMEToUp, 
    double prodPriceToUp, 
    string prodTYPEToUp, 
    int prodMANUToUp, 
    int prodCODEToUp)
{
     DataClassesLINQEPOSDataContext dcld = new DataClassesLINQEPOSDataContext();
     TBLPRODUCT tblprod = (from prod in dcld.TBLPRODUCTs
                           where prod.product_id == selectedIDToUpdate
                           select prod).First();
     tblprod.product_name = prodNAMEToUp;
     tblprod.product_price = prodPriceToUp;
     tblprod.product_type = prodTYPEToUp;
     tblprod.product_manufacturer = prodMANUToUp;
     tblprod.product_code = prodCODEToUp;
     dcld.SubmitChanges();
}

and then when I start to run the program I have this error

"InvalidCastException was unhandled" "Specified cast is not valid."

Sorry I cant post image because I dont have enough reputation "points" :(

this is the control to pass the parameter on my class.

private void btnSaveToUpdate_Click(object sender, EventArgs e)
    {
        if (txtNameToUpdate.Text != "" || 
        txtPriceToUpdate.Text != "" || 
        txtTypeToUpdate.Text != "" || 
        txtCodeToUpdate.Text != "")
        {
            Connection_Products update = new Connection_Products();
            int selctedID = selectedIDToUpdate;
            string prodNAMEToUp = txtNameToUpdate.Text;
            double prodPriceToUp = double.Parse(txtPriceToUpdate.Text);
            string prodTYPEToUp = txtTypeToUpdate.Text;
            int prodMANUToUp = Convert.ToInt32(cmbManufacturerToUpdate.SelectedValue);
            int prodCODEToUp = Convert.ToInt32(txtCodeToUpdate.Text);
            update.updateProduct(selctedID, prodNAMEToUp, prodPriceToUp, 
            prodTYPEToUp, prodMANUToUp, prodCODEToUp);
        }
        else
        {
            MessageBox.Show("Error");
        }
    }
7
  • please post db structure as well Commented Aug 20, 2013 at 10:50
  • i cant post image Sir Amit Agrawal. Commented Aug 20, 2013 at 10:53
  • upload image on imageshack.us and post the link Commented Aug 20, 2013 at 10:55
  • When you debug it at what point in this method does it throw the exception? Commented Aug 20, 2013 at 10:55
  • Or just type out what the data types of the different columns are. You dont need an image for this. Commented Aug 20, 2013 at 10:56

3 Answers 3

1

You most likely have some datatype mismatch between the values that you are trying to store and the data types of the columns in the database. This would be in one of the numeric columns.

If the prodPriceToUp column uses the money data type, this maps to a decimal type in Linq2Sql. You are trying to cast the double for the input parameter to decimal, which may be causing this issue. Try converting prodPriceToUp to a decimal before saving it.

Sign up to request clarification or add additional context in comments.

3 Comments

i used money datatype for "prodPriceToUp"... and double for my parameter.
I already fixed the problem, maybe because of data type from database and DBML connection. I just deleted my DBML and create another one and now it's working.
If you found that this answer led you to solve your problem, please accept the answer
0

I guess you are missing something in type mapping between updateProduct params and properties in tblprod. Please check here you are using correct types

Comments

0

This is because one of the following is trying to cast into an invalid type:

 tblprod.product_name = prodNAMEToUp;
 tblprod.product_price = prodPriceToUp;
 tblprod.product_type = prodTYPEToUp;
 tblprod.product_manufacturer = prodMANUToUp;
 tblprod.product_code = prodCODEToUp;

Check for each of the these that the type of the left side is the same as the type of the right side. for example ensure that "tblprod.product_name" is a string property as you are trying to save a string to that property.

It is also possible that in your LINQ query, the where clause may have this problem, ensure that "prod.product_id" is an integer and not something else like a long or a short.

if these are all ok and you are still getting the exception then you will need to tell use what line is throwing the exception, and give use the details of the structure of tblprod;

3 Comments

I tried to comment out that part of codes then I have same error result.
on a sidenote, you are leaving yourself open to exceptions being thrown when you read in and parse a double from txtpricetoupdate.txt. if the user inputs a non parseable value such as "testvalue" this will fail. You should put in validation such as:
Sorry comment broke! double.tryparse(txtpricetoupdate.txt, out value);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.