Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net中插入表中的数据_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 如何在asp.net中插入表中的数据

C# 如何在asp.net中插入表中的数据,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,现在我找到了一种新的方法,只是需要帮助。基本上,当用户单击“添加到购物车”按钮时,我将从product_desc.aspx页面将产品数据插入数据库。因此,当我运行下面的代码时,我得到了这个错误消息。请帮我解决这个问题,谢谢。谢谢你的帮助 错误: {“对象引用未设置为对象的实例。”} 我的产品描述.aspx页面 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

现在我找到了一种新的方法,只是需要帮助。基本上,当用户单击“添加到购物车”按钮时,我将从product_desc.aspx页面将产品数据插入数据库。因此,当我运行下面的代码时,我得到了这个错误消息。请帮我解决这个问题,谢谢。谢谢你的帮助

错误:

{“对象引用未设置为对象的实例。”}

我的产品描述.aspx页面

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

首先根据需要将Sql列定义为
Binary
VarBinary
数据类型

然后将图像转换为字节数组

    byte[] imageByteArray;
    using(var ms = new MemoryStream())
    {
        yourImage.Save(ms, yourImage.RawFormat);
        imageByteArray = ms.ToArray();
    }
现在你可以做了

cmd.Parameters.AddWithValue("@image", imageByteArray);

首先根据需要将Sql列定义为
Binary
VarBinary
数据类型

然后将图像转换为字节数组

    byte[] imageByteArray;
    using(var ms = new MemoryStream())
    {
        yourImage.Save(ms, yourImage.RawFormat);
        imageByteArray = ms.ToArray();
    }
现在你可以做了

cmd.Parameters.AddWithValue("@image", imageByteArray);

您能在客户端包含更多的代码吗?你能在表单标签中包含IMG标签并发布吗?请现在检查一下,你能在客户端包含更多的代码吗?你能把IMG标签放在一个表单标签里并贴出来吗?请现在检查一下,但我如何从中调用图像。意味着我必须在(yourimage)中输入什么@khans您是在尝试允许用户上载图像还是图像已经在服务器端?@khans是否在属性中?它在文件系统上吗?如果它是一个属性,您可以使用上面的代码,如果它在文件系统上,您可以将其作为二进制文件读取,无需将其转换为图像对象。但是我将如何从中调用图像。意味着我必须在(yourimage)中输入什么@khans您是在尝试允许用户上载图像还是图像已经在服务器端?@khans是否在属性中?它在文件系统上吗?如果它是一个属性,您可以使用上面的代码,如果它在文件系统上,您可以将其作为二进制读取,无需将其转换为图像对象。