0
Answer

Populate listview with VARBINARY(MAX) variable

Ask a question
Dealy

Dealy

10y
1.1k
1
Hello,

I'm trying to populate data from SQL Server into a listview. This is my code in C#:

public void downloadFiles(SqlConnection conn, String sproc, ListView listView, int parentId)
        {       
            SqlCommand command = new SqlCommand(sproc, conn);
            command.Parameters.Add(new SqlParameter("@ParentId", parentId));
            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader rd = null;
            conn.Open();
            rd = command.ExecuteReader();

             while (rd.Read())
             {
                 byte[] b = (byte[])rd["DocPath"];
                 FileStream fs = new FileStream(rd["DocSubject"].ToString(), FileMode.Create);
                 fs.Write(b, 0, b.Length);

                 StreamWriter sw = new StreamWriter(fs);
                
                 ListViewItem item = new ListViewItem(rd["ParentId"].ToString());
                 for (int i = 1; i <= rd.FieldCount - 1; i++)
                 {
                     item.SubItems.Add(rd["DocSubject"].ToString());
                     item.SubItems.Add(rd["DocName"].ToString());
                     item.SubItems.Add(rd["DocFileFormat"].ToString());
                     sw.WriteLine(item.SubItems.Add(rd["DocSubject"].ToString()));

                     sw.Flush();
                     sw.Close();

                 }
                 listView.Items.Add(item);
             }
            rd.Close();
            conn.Close();
        }


The problem is that I need DocPath (VARBINARY(MAX) variable) to be populated on column DocPath of the listView as a download link.

I would appreciate any kind of help.

Thank you in advance.