Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
So in my windows form I've made a textbox which the user uses to enter the website address and then click a button to screen scrap that website into a browser control. They can also save that screen scraped website to an sql database. What I want to do now is read that data from the sql database onto the web browser control. Now the screen scraped page is saved to database as an image and is display in the database as binary data. I have some of the code for this, but i'm not sure how to read it to the browser control.


Here is my code

The code that I have done so far for it is displayed in the method for btnRead


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Data.SqlClient;
 
namespace FilmAndEntertainmentSystem
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private string GetWebsiteHtml(string url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            string result = reader.ReadToEnd();
            stream.Dispose();
            reader.Dispose();
            return result;
        }
 
        private void btnGetHTML_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
        }
 
        private void btnScreenSave_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
 
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(html);
 
            // set up data connection

            SqlConnection cs = new SqlConnection("Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True");
 
            // Set up adapter manager

            SqlDataAdapter da = new SqlDataAdapter();
 
            using (SqlCommand com = new SqlCommand("INSERT INTO Website (WebsiteImage) VALUES (@Image)", cs))
            {
                com.Parameters.AddWithValue("@Image", bytes);
                cs.Open();
 
                com.ExecuteNonQuery();
 
                cs.Close();
            }
        }
 
        private void btnRead_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
 
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(html);
 
            // set up data connection

            SqlConnection cs = new SqlConnection("Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True");
 
            // Set up adapter manager

            SqlDataAdapter da = new SqlDataAdapter();
 
            // Data set
            DataSet ds = new DataSet();
 
            da.SelectCommand = new SqlCommand("Select WebsiteImage From Website Where WebsiteID = 3", cs);
 
            da.Fill(ds, "Website");
 
            cs.Open();

            response.ContentType = "Image";
            response.BinaryWrite(bytes);
 
            
            cs.Close();
 

 
        }
    }
}
Posted
Comments
ArtificerGM 23-May-11 18:40pm    
Do the requirements of your code DEMAND a browser control? I ask because if you are using a broswer control then obviously you're using windows forms and if that's the case the PictureBox control's Image property can easily take a new Bitmap instance from a stream and display it no problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900