0

Now I found a new way just need help on it please. Basically, I am to insert a product data into database from product_desc.aspx page when the user clicks of add to cart button. So when I run the following code I get this error message. Please help me to solve this problem, Thanks. Help is appreciated.

Error:

{"Object reference not set to an instance of an object."}

my Product_desc.aspx page

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Repeater ID="d1" runat="server">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
         <div style="height:300px; width:600px; border:1px solid black; margin-left:250px;">
        <div style="height:300px; width:200px; float:left; border:1px solid black;">
            <img src="data:image;base64,<%# Convert.ToBase64String((byte[])Eval("Image")) %>" />
        </div>
        <div style="height:300px; width:350px; float:left; border:1px solid black;">
             Coffee Name:
            <asp:Label ID="CoffeeNameLabel" runat="server" Text='<%# Eval("CoffeName") %>' />
            <br />
            Coffee Strength
            <asp:Label ID="CoffeeStrengthLabel" runat="server" Text='<%# Eval("CoffeeStrength") %>' />
            <br />
            Coffee Grind
            <asp:Label ID="CoffeeGrindLabel" runat="server" Text='<%# Eval("CoffeeGrind") %>' />
            <br />
            Origin
            <asp:Label ID="CoffeeOriginLabel" runat="server" Text='<%# Eval("Origin") %>' />
            <br />
            Quantity
            <asp:Label ID="CoffeeQuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />
            <br />
            Price
            <asp:Label ID="CoffeePriceLabel" runat="server" Text='<%# Eval("Price") %>' />
        </div>
        </ItemTemplate>
        <FooterTemplate>
        </FooterTemplate>
    </asp:Repeater>
    <br />
    <asp:Button ID="b1" runat="server" Text="Add To Cart" onClick="b1_Click" />
</asp:Content>

Product_desc.aspx.cs page code

protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                Response.Redirect("ordercoffee.aspx");
            }
            else
            {
                id = Convert.ToInt32(Request.QueryString["id"].ToString());
                cons.Open();
                SqlCommand cmd = cons.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from coffeeshop where Id="+id+"";
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                d1.DataSource = dt;
                d1.DataBind();
                cons.Close();
            }
        }

        protected void b1_Click(object sender, EventArgs e)
        {
           //double p = double.Parse((string)ViewState["Price"]);
            string cimg = ViewState["Image"].ToString();
            string  cname = ViewState["CoffeName"].ToString();
            string cstrength = ViewState["CoffeeStrength"].ToString();
            string cgrind = ViewState["CoffeeGrind"].ToString();
            string corigin = ViewState["Origin"].ToString();
            string cprice = ViewState["Price"].ToString();
            string cquantity = ViewState["Quantity"].ToString();
            //string s2 = System.Web.HttpContext.Current.User.Identity.Name;
            string s1 = Request.QueryString["id"];
                cons.Open();    
                SqlCommand cmd = cons.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO cart (CoffeeOrderId, CoffeeName, Strength, Grind, Origin, Quantity, Price, Image) VALUES(@orderid, @name, @strength, @grind, @origin, @quantity, @price, @image)";
                cmd.Parameters.AddWithValue("@orderid", s1);
                cmd.Parameters.AddWithValue("@name", cname);
                cmd.Parameters.AddWithValue("@strength", cstrength);
                cmd.Parameters.AddWithValue("@grind", cgrind);
                cmd.Parameters.AddWithValue("@origin", corigin);
                cmd.Parameters.AddWithValue("@quantity", cquantity);
                cmd.Parameters.AddWithValue("@price", cprice);
                cmd.Parameters.AddWithValue("@image", cimg);
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                cons.Close();
                Response.Redirect("login.aspx");

        }
2
  • can you include a little more code on the client side? can you just include the IMG tag inside a FORM tag and Post it? Commented Mar 9, 2017 at 22:51
  • Just updated pleae check it now Commented Mar 9, 2017 at 22:57

1 Answer 1

2

First define the Sql Column to Binary or VarBinary data type depending on your need

then convert the image to byte array

    byte[] imageByteArray;
    using(var ms = new MemoryStream())
    {
        yourImage.Save(ms, yourImage.RawFormat);
        imageByteArray = ms.ToArray();
    }

now you can just do

cmd.Parameters.AddWithValue("@image", imageByteArray);
Sign up to request clarification or add additional context in comments.

3 Comments

But how will I call Image from <asp:Image ID="Image1" runat="server" ImageUrl="data:image;base64,<%# Convert.ToBase64String((byte[])Eval("Image")) %>" />. Means what will I have to to put in (yourimage)
@khans are you trying to allow the user to upload the image or is the image on the server side already?
@khans is it in a property? is it on the file system? if its a property you can just use the code above, if its on the file system you can just read it as binary, no need to convert it into image object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.