图像更新与sql数据库中的其他相关记录

时间:2017-06-08 21:42:04

标签: c# sql asp.net image gridview

我有一个名为Phase的表,它包含其他列和图像列。一切正常,但是当我想要更新图像时,它说“我不能隐式地将nvarchar转换为varbinary(max)”,当我想要更新除图像之外的任何一列时,它会向我显示相同的错误。请帮我解决我做错了什么?下面分别是我的aspx和aspx.cs代码:

protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int index = e.RowIndex;

    GridViewRow row = (GridViewRow)gv1.Rows[index];

    Label eid = (Label)row.FindControl("lbleid");
    TextBox date = (TextBox)row.FindControl("txtdate");
    DropDownList ddl1 = (DropDownList)row.FindControl("ProjectID");
    DropDownList ddl2 = (DropDownList)row.FindControl("ProjectTypeID");
    DropDownList ddl3 = (DropDownList)row.FindControl("StateID");
    DropDownList ddl4 = (DropDownList)row.FindControl("LGAID");
    DropDownList ddl5 = (DropDownList)row.FindControl("CommunityID");
    DropDownList ddl6 = (DropDownList)row.FindControl("CoyID");

    FileUpload fu = (FileUpload)row.FindControl("fu1");

    foreach (HttpPostedFile postedFile in fu.PostedFiles)

    {
        string filename = Path.GetFileName(postedFile.FileName);
        string contentType = postedFile.ContentType;
        using (Stream fs = postedFile.InputStream)

        {
            using (BinaryReader br = new BinaryReader(fs))
            {

                byte[] bytes = br.ReadBytes((Int32)fs.Length);

                SqlCommand cmd = new SqlCommand("update Phase set Photo = '" + fu.FileName + "' where Phase_1ID=" + Convert.ToInt32(eid.Text) + "", con);

                con.Open();
                int res = cmd.ExecuteNonQuery();
                con.Close();
            }

            SqlCommand cmd1 = new SqlCommand("update Phase set DateOfVisit='" + date.Text + "', ProjectTitle='" + ddl1.Text + "', Type='" + ddl2.Text + "', StateName='" + ddl3.Text + "', LGName='" + ddl4.Text + "', CommunityName='" + ddl5.Text + "', CompanyName='" + ddl6.Text + "", con);
            con.Open();
            int res1 = cmd1.ExecuteNonQuery();
            con.Close();

            if (res1 == 1)
            {

                Response.Write("<script>alert('Updation done!')</script>");

            }

            gv1.EditIndex = -1;

            gv1.DataBind();

        }
    }
}

}

0 个答案:

没有答案