Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,I got a error.please help me to resolve it:
I am Creating a shopping cart project.When I click on Add to cart Button.Then all the datails saved in a datatablee image Session.But when i am storing the image I got this Error:


C#
protected void Cartbtn_Click(object sender, EventArgs e)
        {

           
      
                ProductDescriptionObject.SelectedProductSNo = Convert.ToInt16(ProductSNo);

                DataTable dtProducts = ProductDescriptionObject.GetProductsOnTheBaseOfProductId();
                  
              
                DataTable dt = new DataTable(); //storing all of the records

                dt.Columns.Add("ProductSNo", typeof(string)); // adding the columns
                dt.Columns.Add("ProductName", typeof(string));
                dt.Columns.Add("ProductDescription", typeof(string));
                dt.Columns.Add("ProductPrice", typeof(int));
                dt.Columns.Add("ProductImage", typeof(Image));
                dt.Columns.Add("ProductQuantity", typeof(string));
              //  dt.Columns.Add("AvailableStock", typeof(string));

                DataRow dr = dt.NewRow(); //adding the rows
                dr["ProductSNo"] = ProductSNo;
                dr["ProductName"] = Convert.ToString(dtProducts.Rows[0]["ProductName"]);
                dr["ProductDescription"] = Convert.ToString(dtProducts.Rows[0]["ProductDescription"]);
                dr["ProductPrice"] = (dtProducts.Rows[0]["ProductPrice"]);
               dr["ProductImage"] = (dtProducts.Rows[0]["ProductImage"]);
                dr["ProductQuantity"] = ProductQuantity;
              //  dr["AvailableStock"] = lblAvailableStock.Text;

                dt.Rows.Add(dr); //adding the data row in the data table. 

                Session["MyCart"] = dt; //asigning the datatable in the session.
                ItemsCountInCartLinkBtn.Text = dt.Rows.Count.ToString();
            }
           }
Posted
Updated 20-Sep-15 19:46pm
v3
Comments
Naveen.Sanagasetti 21-Sep-15 2:22am    
What about datatype of dtProducts.Rows[0]["ProductImage"], I guess the problem is conversion. Please check the left & right hand side datatypes both should be same then only it should work perfect.
Member 11804811 23-Sep-15 2:29am    
Thankyou Naveen....

DataTable does not accept Image data type. Please see: DataColumn.DataType Property[^]. If you want to work with images, it should be byte[] data type.

I'd suggest to read this: Retrieving Binary Data[^] and this: ow to convert image into byte[] and byte[] to image using C# in ASP.NET[^]

[EDIT]
Other resources:
Image Handling In ASP.NET[^]
How can i add image in a datatable?[^]
DataType for Saving Images in DataColumn of DataTable[^]
 
Share this answer
 
v2
Comments
Member 11804811 23-Sep-15 2:26am    
Thankyou Maciej..........
Maciej Los 24-Sep-15 1:01am    
You're very welcome.
I find the Error...

DataTable dt = new DataTable(); //storing all of the records

dt.Columns.Add("ProductSNo", typeof(string)); // adding the columns
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("ProductDescription", typeof(string));
dt.Columns.Add("ProductPrice", typeof(int));
dt.Columns.Add("ProductImage", typeof(Image)); <--Here I define Type Image It should be Byte[]-->
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900