Dharam Rai
Dharam Rai

Reputation: 81

How to insert image from gridview to another table in database

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 HorizontalAlign="Left" 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();
                        }
                    }
                }
            }

Upvotes: 1

Views: 354

Answers (2)

Dharam Rai
Dharam Rai

Reputation: 81

After beating head everywhere atlast I figured out and I am posting it incase if someone may refer to. Thanks to @Crowcoder for giving a logic to make it happen.

foreach (GridViewRow row1 in gvImage.Rows)
                {
                    if (row1.RowType == DataControlRowType.DataRow)
                    {
                        string Id = gvImage.DataKeys[row1.RowIndex].Value.ToString();
                        ImageButton imgbtn = (ImageButton)gvImage.Rows[row1.RowIndex].FindControl("Image2");
                        string filename = imgbtn.ImageUrl;

                        TextBox ftype = (row1.FindControl("txtFileType") as TextBox);

                        byte[] bytes = (byte[])GetData("SELECT BData FROM Employee WHERE Id =" + txt_StaffId.Text).Rows[0]["BData"];
                        string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                        imgbtn.ImageUrl = "data:image/png;base64," + base64String;

                        {

                            string constr = ConfigurationManager.ConnectionStrings["CONNECTION"].ConnectionString;
                            using (SqlConnection con8 = new SqlConnection(constr))
                            {
                                string query = "insert into SShare (FId,UDetails,ShareBy,ShareByUserId,BData,ImageName, FileType) values(@FId,@UDetails,@ShareBy,@ShareByUserId,@BData,@ImageName,@FileType)";

                                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", bytes);

                                    cmd8.Parameters.AddWithValue("@ImageName", filename);
                                    cmd8.Parameters.AddWithValue("@FileType", ftype.Text);

                                    con8.Open();
                                    // cmd8.ExecuteNonQuery();
                                    this.ExecuteQuery(cmd8, "SELECT");

                                    con8.Close();
                                }


                            }
                        }
                    }
                }

Upvotes: 1

Crowcoder
Crowcoder

Reputation: 11514

Here is what I suggest. You could get all the data out of the grid, but you can also just do it in SQL.

Notice BDAta is NOT a SqlParameter, it is pulled from the Employee table:

INSERT INTO [SShare](FId,UDetails,ShareBy,ShareByUserId,BData,FileType,ImageName)
    SELECT @FId, @UDetails, @ShareBy, @ShareByUserId, BData, @FileType, @ImageName
    FROM Employee
    WHERE FId = @FId;

Upvotes: 2

Related Questions