Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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# 未从数据库在gridview中绑定图像_C#_Asp.net_Gridview_Sql Server 2008 R2 - Fatal编程技术网

C# 未从数据库在gridview中绑定图像

C# 未从数据库在gridview中绑定图像,c#,asp.net,gridview,sql-server-2008-r2,C#,Asp.net,Gridview,Sql Server 2008 R2,下面是我的代码。。。我正在尝试将图像存储在数据库中。我使用varbinary(MAX)数据类型在数据库中存储数据字节。请告诉我哪里出了问题 int qtype = Convert.ToInt32(ddl_q_Type.SelectedValue); var getQ = (from q in obj.QuestionRegistrations orderby q.QueCode ascending

下面是我的代码。。。我正在尝试将图像存储在数据库中。我使用varbinary(MAX)数据类型在数据库中存储数据字节。请告诉我哪里出了问题

    int qtype = Convert.ToInt32(ddl_q_Type.SelectedValue);
                var getQ = (from q in obj.QuestionRegistrations
                            orderby q.QueCode ascending
                            where q.QueQuesType == qtype && q.QueLanguageCode == 1
                            select new { q.QueCode, q.QueQuestion }
                                ).ToArray();


                index = Convert.ToInt32(ViewState["index"].ToString()) + 1;
                ViewState["index"] = index;
                previosIndex = index - 1;
                ViewState["previosIndex"] = previosIndex;
                txt_question.Text = getQ[index].QueQuestion;
                lblqcode.Text = getQ[index].QueCode.ToString();


                int qcode = Convert.ToInt32(lblqcode.Text);
                var options = (from opt in obj.QuestionAndOptionsMappings
                               where opt.QueMapQuestionCode == qcode
                               select new { opt.QueMapOptions, opt.QueMapCorrectAnswer, opt.QueMapId, opt.QueMapImage }
                                   ).ToList();


                DataTable dt = new DataTable();

                dt.Columns.Add("QueMapOptions", typeof(string));
                dt.Columns.Add("QueMapId", typeof(string));
                dt.Columns.Add("QueMapImage", typeof(string));

                foreach (var y in options)
                {
                    DataRow dr = dt.NewRow();
                    dr["QueMapOptions"] = y.QueMapOptions;
                    dr["QueMapId"] = y.QueMapId;
                    dr["QueMapImage"] = (Byte[])y.QueMapImage;

                    dt.Rows.Add(dr);
                }

 GridView1.DataSource = dt;
            GridView1.DataBind();
此代码用于将图像存储在数据库中

for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                FileUpload fp = (FileUpload)GridView1.Rows[i].Cells[2].FindControl("fp");
                Label lblmapid = (Label)GridView1.Rows[i].Cells[1].FindControl("lblmapid");
                bool e1 = fp.HasFile;
                int mapiidd = Convert.ToInt32(lblmapid.Text);
                if (fp.HasFile)
                {
                    string path = Server.MapPath("~/Uploads/") + fp.FileName;
                    fp.SaveAs(path);
                    byte[] imageBytes =
                        File.ReadAllBytes(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/") + fp.FileName);
                    int qcodelbl = Convert.ToInt32(lblqcode.Text);
                    QuestionAndOptionsMapping qmap = new QuestionAndOptionsMapping();

                    var update = obj.QuestionAndOptionsMappings.Where(q => q.QueMapId == mapiidd && q.QueMapQuestionCode == qcodelbl)
                        ;
                    update.SingleOrDefault().QueMapImage = imageBytes;
                    obj.SaveChanges();
                }

            }
for(int i=0;iq.QueMapId==mapiidd&&q.quemapsquestioncode==qcodelbl)
;
update.SingleOrDefault().QueMapImage=imageBytes;
obj.SaveChanges();
}
}
ASPX代码

<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField HeaderText="OPTIONS" HeaderStyle-Width="55%">
                                <ItemTemplate>
                                    <asp:TextBox ID="Option" runat="server" Text='<%#Eval("QueMapOptions") %>' Height="40px"
                                        Width="500px"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%" Visible="false">
                                <ItemTemplate>
                                    <asp:Label ID="lblmapid" runat="server" Text='<%#Eval("QueMapId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%">
                                <ItemTemplate>
                                    <asp:CheckBox ID="Chk_correct_Ans" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%">
                                <ItemTemplate>
                                    <asp:FileUpload ID="fp" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%">
                                <ItemTemplate>
                                    <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("QueMapImage") %>' Height="80px"
                                        Width="100px" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

好的,您不能只将二进制列绑定到
GridView
列。您需要使用
Response.BinaryWrite
方法

这里有几个步骤可以实现你想要的

  • 创建一个通用处理程序来读取二进制数据。我们将其命名为ImageHandler.ashx。确保您拥有要传递到此处理程序的表的主键。编写如下所示的处理程序代码(只是一个示例)

    string connectionString=ConfigurationManager.connectionString[“DBConnection”].connectionString

            SqlConnection conn = new SqlConnection(connectionString);
    
            SqlCommand cmd = new SqlCommand();
    
            cmd.CommandText = "Select [Content] from Images where ID =@ID";
    
            cmd.CommandType = CommandType.Text;
    
            cmd.Connection = conn;
    
            SqlParameter ImageID = new SqlParameter("@ID", SqlDbType.BigInt);
            ImageID.Value = context.Request.QueryString["ID"];
            cmd.Parameters.Add(ImageID);
            conn.Open();
            SqlDataReader dReader = cmd.ExecuteReader();
            dReader.Read();
            context.Response.BinaryWrite((byte[])dReader["Content"]);
            dReader.Close();
            conn.Close();
        }
    
  • 然后像这样调用处理程序

    ImageUrl=“”

  • 而不是

    ImageUrl='<%#Eval("QueMapImage") %>'
    
    ImageUrl=''
    
    这是一个完整的示例,我从中提取了上述示例


    祝你一切顺利

    你的图像正确地保存到数据库中了吗?@Sam:是的。。。。它是以二进制存储的。与其让我们猜出哪里出了问题,为什么不解释一下你的错误呢?@paqogomez:我没有收到任何错误。。图像正在被绑定。。但是图像是不可见的。。那是it@Sam:请查看我的数据库表屏幕截图的编辑如果我有超过1行,则网格中其余行仅显示第一行的图像。不可能是因为您一定做错了什么。使用codeAdding更新您的问题@Sam Answer创建处理程序后,您必须在处理程序中获取所需上下文的QueMapID。Request.QueryString[“QueMapID”];在处理程序中,然后进行DB调用,在网格视图中,ImageUrl=''
    ImageUrl='<%#Eval("QueMapImage") %>'