Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here's my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Project_IT191P
{
    public partial class view_cart : System.Web.UI.Page
    {
        string s;
        string t;
        string[] a = new string[8];
        int tot = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[8] { new DataColumn("ProductName"), new DataColumn("Description"), new DataColumn("Price"), new DataColumn("Stocks"), new DataColumn("Category"), new DataColumn("ImageURL"), new DataColumn("ProductID"), new DataColumn("id") });

            if (Request.Cookies["cookie"] != null)
            {
                s = Convert.ToString(Request.Cookies["cookie"].Value);

                string[] strArr = s.Split('|');

                for (int i = 0; i < strArr.Length; i++)
                {
                    t = Convert.ToString(strArr[i].ToString());
                    string[] strArr1 = t.Split(',');
                    for (int j = 0; j < strArr1.Length; j++)
                    {
                        a[j] = strArr1[j].ToString();
                    }

                    dt.Rows.Add(a[0].ToString(), a[1].ToString(), a[2].ToString(), a[3].ToString(), a[4].ToString(), a[5].ToString(), i.ToString(), a[6].ToString());

                    tot = tot + (Convert.ToInt32(a[2].ToString()) * Convert.ToInt32(a[3].ToString()));
                }

            }

            d1.DataSource = dt;
            d1.DataBind();

            l1.Text = tot.ToString();
        }

       
    }
}


What I have tried:

it says that
tot = tot + (Convert.ToInt32(a[2].ToString()) * Convert.ToInt32(a[3].ToString()));
input string was not in correct format
Posted
Comments
George Swan 22-Mar-18 2:00am    
Looks like a[2] or a[3] is an empty string. Also, you don't need to call ToString() on strings
F-ES Sitecore 22-Mar-18 5:40am    
You're trying to convert test (a[2] or a[3]) to an int but the text can't be converted to an int. We don't know what that text is so can't say why, you'll need to use the debugger for that. We can't tell you how to handle what happens when a conversion isn't possible as we don't know your business rules.

If "a" should only be able to hold int then make it an array of int instead of string and do the version to int when the array is populated.

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