Click here to Skip to main content
16,022,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was told to reference default1 to default2 once upload button has been clicked,
it returns invalid operation exception was unhandled by user code
Fill: SelectCommand.Connection property has not been initialized.

Default1
C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;


namespace Defult
{
    public partial class _Default : System.Web.UI.Page
    {
        //SqlConnection sqlcon = new SqlConnection("PDF_Files");
        SqlCommand sqlcmd = new SqlCommand();
        //SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                LoadGrid();
            }
            Label1.Text = "";

        }

        void LoadGrid()
    {
        //SqlConnection sqlcon = new SqlConnection("PDF_Files");
        SqlCommand sqlcmd = new SqlCommand("select * from Files");
       SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        //sqlcon.Close();
    }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //PDF Upload Code to SQL SERVER database table
            if (FileUpload1.HasFile)
            {
                Stream fs = default(Stream);
                fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br1 = new BinaryReader(fs);
                byte[] pdfbytes = br1.ReadBytes(FileUpload1.PostedFile.ContentLength);
                
                SqlCommand sqlcmd = new SqlCommand("insert into Files(pdfname,pdfcontent) values (@pdfname,@pdfcontent)");
                sqlcmd.Parameters.Add("@pdfname", FileUpload1.FileName);
                sqlcmd.Parameters.Add("@pdfcontent", pdfbytes);
                sqlcmd.ExecuteNonQuery();
                //sqlcon.Close();
                Label1.Text = "Successfully pdf upload to SQL Server database.";
                LoadGrid();
            }

        }


    }
}




Default2
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Defult
{
    public partial class Default2 : System.Web.UI.Page
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["PDF_Files"].ConnectionString);
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        string qstr;
        byte[] b = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                qstr = Request.QueryString["ID"];
                //Read PDF file from DATABASE table pdf field
                SqlCommand sqlcmd = new SqlCommand("Select pdfcontent from pdfupload where ID='@ID" + qstr + "'", sqlcon); 
                //use condition to retrieve particulatr PDF
                sqlcon.Open();
                da = new SqlDataAdapter(sqlcmd);
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    b = ((byte[])dt.Rows[0][0]);
                    //Collect Bytes from database and write in Webpage
                    Response.ContentType = "application/pdf";
                    Response.BinaryWrite(b);
                }
            }
        }
    }
}
Posted
Updated 5-Sep-11 0:55am
v2

1 solution

You need to add the sqlcon code from Default2 to Default1 - at the moment the SqlCommand does not know what SqlConnection to use.
 
Share this answer
 

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