下面是我的用户界面和表格。我想在gridview中插入一个孩子的图像,它也将保存在数据库中。请协助我想如何实现这一目标。
表上图像的数据类型是varchar(MAX)。
这是我到目前为止所做的。
(C#代码)
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;
using System.IO;
using System.Configuration;
namespace MyProject
{
public partial class managechild : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lolConnectionString1"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
string session = System.Web.HttpContext.Current.User.Identity.Name;
Response.Cookies["uname"].Value = session;
DataSet ds = new DataSet();
conn.Open();
string cmdstr = "SELECT * from child c join parent p on c.ParentId = p.ParentId join login l on l.Username = p.UserId where l.Username ='" + session + "'";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
gvUpload.DataSource = ds;
gvUpload.DataBind();
conn.Close();
}
protected void btnUpload_OnClick(object sender, EventArgs e)
{
TextBox txtName = (TextBox)gvUpload.SelectedRow.FindControl("ChildName");
FileUpload fuploadFile = (FileUpload)gvUpload.SelectedRow.FindControl("fUpload");
Button btnUpload = (Button)gvUpload.SelectedRow.FindControl("btnUpload");
if (fuploadFile.HasFile)
{
string fileName = fuploadFile.FileName;
string exten = Path.GetExtension(fileName);
//here we have to restrict file type
exten = exten.ToLower();
string[] acceptedFileTypes = new string[4];
acceptedFileTypes[0] = ".jpg";
acceptedFileTypes[1] = ".jpeg";
acceptedFileTypes[2] = ".gif";
acceptedFileTypes[3] = ".png";
bool acceptFile = false;
for (int i = 0; i <= 3; i++)
{
if (exten == acceptedFileTypes[i])
{
acceptFile = true;
}
}
if (!acceptFile)
{
lblMsg.Text = "The file you are trying to upload is not a permitted file type!";
}
else
{
//upload the file onto the server
fuploadFile.SaveAs(Server.MapPath("~/images/child/"+fileName));
conn.Open();
string cmdstr = "insert into Child (Image) values (@photo)";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("@photo", "images/child/"+fileName);
cmd.ExecuteNonQuery();
conn.Close();
BindGrid();
}
}
}
}
}
用户界面代码 (asp.net)
<asp:GridView ID="gvUpload" runat="server" AutoGenerateColumns="False"
ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None"
style="margin-left: 128px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Child Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#DataBinder.
Eval(Container.DataItem, "ChildName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="imgPhoto" runat="server" Width="100px" Height="120px"
ImageUrl='<%#DataBinder.Eval(Container.DataItem, "Image") %>' />
</ItemTemplate>
<ItemTemplate>
<asp:FileUpload ID="fUpload" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_OnClick" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>