简体   繁体   中英

How to insert data in table in asp.net

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");

        }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM