Dharam Rai

Dharam Rai

  • NA
  • 383
  • 22.6k

Insert image from gridview to another table in database

Apr 17 2017 8:57 AM
Dear all I am displaying an image in my gridview, this image is saved in database in varbinary format, with its content type and image name. And my image in gridview is displaying perfect, now I want to insert this same image from gridview to another table from the button click outside the gridview, How do I achieve this can anyone please guide me? I tried achieving it by receiving this image data from gridview such as Varbinary data - which is an image in database and content type and imagename into textbox but it thorws an error "Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query"
 
<asp:TemplateField HeaderText="" ItemStyle-Width="" Visible="true">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" class="preview" ToolTip='<%#Bind("StaffName") %>'
NavigateUrl='' runat="server">
<asp:ImageButton runat="server" ID="Image2" class="img2" ImageUrl='<%# Eval("ImageName") %>'
CommandName='<%# Eval("Id") %>' CommandArgument='<%# Eval("ImageName") %>' />
</asp:HyperLink>
<asp:TextBox ID="txtFileType" runat="server" Text='<%# Eval("FileType") %>' Visible="true"></asp:TextBox>
<asp:TextBox ID="txtBData" runat="server" Text='<%# Eval("BData") %>' Visible="true"></asp:TextBox>
<asp:TextBox ID="txtImageName" runat="server" Text='<%# Eval("ImageName") %>' Visible="true"></asp:TextBox>
<br />
<br />
</ItemTemplate>
<ControlStyle Width="100%" />
<HeaderStyle Horizontal VerticalAlign="Middle" Width="10%" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="20%" />
</asp:TemplateField>
 
 
foreach (GridViewRow row1 in gvImage.Rows)
{
if (row1.RowType == DataControlRowType.DataRow)
{
// txtFileType
// txtBData
// txtImageName
TextBox txtFileType, txtBData, txtImageName;
txtFileType = (row1.Cells[1].FindControl("txtFileType") as TextBox);
txtBData = (row1.Cells[1].FindControl("txtBData") as TextBox);
txtImageName = (row1.Cells[1].FindControl("txtImageName") as TextBox);
string constr = ConfigurationManager.ConnectionStrings["CONNECTION"].ConnectionString;
using (SqlConnection con8 = new SqlConnection(constr))
{
string query = "insert into SShare (FId,UDetails,ShareBy,ShareByUserId,BData,FileType,ImageName) values(@FId,@UDetails,@ShareBy,@ShareByUserId,@BData,@FileType,@ImageName)";
using (SqlCommand cmd8 = new SqlCommand(query))
{
cmd8.Parameters.AddWithValue("@FId", txt_Tester.Text);
cmd8.Parameters.AddWithValue("@UDetails", TextBox1.Text);
cmd8.Parameters.AddWithValue("@ShareBy", txt_StaffId.Text);
cmd8.Parameters.AddWithValue("@ShareByUserId", txt_Employee.Text);
cmd8.Parameters.AddWithValue("@BData", txtBData.Text);
cmd8.Parameters.AddWithValue("@FileType", txtFileType.Text);
cmd8.Parameters.AddWithValue("@ImageName", txtImageName.Text);
con8.Open();
// cmd8.ExecuteNonQuery();
this.ExecuteQuery(cmd8, "SELECT");
con8.Close();
}
}
}
}
 

Answers (2)