Could not load type 'Default.DataMatch' in DataMatch.aspx file

Posted by salvationishere on Stack Overflow See other posts from Stack Overflow or by salvationishere
Published on 2010-05-22T00:36:07Z Indexed on 2010/05/22 0:40 UTC
Read the original article Hit count: 355

I am developing a C# VS 2008 / SQL Server 2008 website, but now I am getting the above error when I build it.

I included the Default.aspx, Default.aspx.cs, DataMatch.aspx, and DataMatch.aspx.cs files below. What do I need to do to fix this?

Default.aspx:

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" Title="Untitled Page" %>
...

DataMatch.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataMatch.aspx.cs" Inherits="_Default.DataMatch" %>
...

Default.aspx.cs:

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.Collections.Generic;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Data.SqlClient;
using ADONET_namespace;
using System.Security.Principal;
//using System.Windows;

public partial class _Default : System.Web.UI.Page

//namespace AddFileToSQL
{
        //protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;
        //protected System.Web.UI.HtmlControls.HtmlInputButton btnOWrite;
        //protected System.Web.UI.HtmlControls.HtmlInputButton btnAppend;
        protected System.Web.UI.WebControls.Label Label1;
        protected static string inputfile = "";
        public static string targettable;
        public static string selection;

        // Number of controls added to view state
        protected int default_NumberOfControls
        {
            get
            {
                if (ViewState["default_NumberOfControls"] != null)
                {
                    return (int)ViewState["default_NumberOfControls"];
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                ViewState["default_NumberOfControls"] = value;
            }
        }

        protected void uploadFile_onclick(object sender, EventArgs e)
        {
        }

        protected void Load_GridData()
        {
            //GridView1.DataSource = ADONET_methods.DisplaySchemaTables();
            //GridView1.DataBind();
        }

        protected void btnOWrite_Click(object sender, EventArgs e)
        {
            if (uploadFile.PostedFile.ContentLength > 0)
            {
                feedbackLabel.Text = "You do not have sufficient access to overwrite table records.";
            }
            else
            {
                feedbackLabel.Text = "This file does not contain any data.";
            }
        }

        protected void btnAppend_Click(object sender, EventArgs e)
        {
            string fullpath = Page.Request.PhysicalApplicationPath;

            string path = uploadFile.PostedFile.FileName;

            if (File.Exists(path))
            {
                // Create a file to write to.
                try
                {
                    StreamReader sr = new StreamReader(path);
                    string s = "";
                    while (sr.Peek() > 0)
                        s = sr.ReadLine();
                    sr.Close();
                }
                catch (IOException exc)
                {
                    Console.WriteLine(exc.Message + "Cannot open file.");
                    return;
                }
            }

            if (uploadFile.PostedFile.ContentLength > 0)
            {

                inputfile = System.IO.File.ReadAllText(path);
                Session["Message"] = inputfile;
                Response.Redirect("DataMatch.aspx");
            }
            else
            {
                feedbackLabel.Text = "This file does not contain any data.";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.IsAuthenticated)
            {
                WelcomeBackMessage.Text = "Welcome back, " + User.Identity.Name + "!";

                // Reference the CustomPrincipal / CustomIdentity
                CustomIdentity ident = User.Identity as CustomIdentity;
                if (ident != null)
                    WelcomeBackMessage.Text += string.Format(" You are the {0} of {1}.", ident.Title, ident.CompanyName);

                AuthenticatedMessagePanel.Visible = true;
                AnonymousMessagePanel.Visible = false;
                if (!Page.IsPostBack)
                {
                    Load_GridData();
                }                                
            }
            else
            {
                AuthenticatedMessagePanel.Visible = false;
                AnonymousMessagePanel.Visible = true;
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            targettable = row.Cells[2].Text;
        }
    }

DataMatch.aspx.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADONET_namespace;
//using MatrixApp;

//namespace AddFileToSQL
//{
    public partial class DataMatch : AddFileToSQL._Default
    {
        protected System.Web.UI.WebControls.PlaceHolder phTextBoxes;
        protected System.Web.UI.WebControls.PlaceHolder phDropDownLists;
        protected System.Web.UI.WebControls.Button btnAnotherRequest;
        protected System.Web.UI.WebControls.Panel pnlCreateData;
        protected System.Web.UI.WebControls.Literal lTextData;
        protected System.Web.UI.WebControls.Panel pnlDisplayData;

        protected static string inputfile2;
        static string[] headers = null;
        static string[] data = null;
        static string[] data2 = null;
        static DataTable myInputFile = new DataTable("MyInputFile");
        static string[] myUserSelections;
        static bool restart = false;
        private DropDownList[] newcol;
        int @temp = 0;
        string @tempS = "";
        string @tempT = "";

        // a Property that manages a counter stored in ViewState
        protected int NumberOfControls
        {
            get { return (int)ViewState["NumControls"]; }
            set { ViewState["NumControls"] = value; }
        }

        private Hashtable ddl_ht
        {
            get
            {
                return (Hashtable)ViewState["ddl_ht"];
            }
            set
            {
                ViewState["ddl_ht"] = value;
            }
        }

        // Page Load 
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddl_ht = new Hashtable();
                this.NumberOfControls = 0;
            }
        }

        // This data comes from input file 
        private void PopulateFileInputTable()
        {
            myInputFile.Columns.Clear();
            string strInput, newrow;
            string[] oneRow;
            DataColumn myDataColumn;
            DataRow myDataRow;
            int result, numRows;

            //Read the input file
            strInput = Session["Message"].ToString();
            data = strInput.Split('\r');

            //Headers
            headers = data[0].Split('|');

            //Data
            for (int i = 0; i < data.Length; i++)
            {
                newrow = data[i].TrimStart('\n');
                data[i] = newrow;
            }
            result = String.Compare(data[data.Length - 1], "");
            numRows = data.Length;

            if (result == 0)
            { numRows = numRows - 1; }

            data2 = new string[numRows];

            for (int a = 0, b = 0; a < numRows; a++, b++)
            {
                data2[b] = data[a];
            }

            // Create columns
            for (int col = 0; col < headers.Length; col++)
            {
                @temp = (col + 1);
                @tempS = @temp.ToString();
                @tempT = "@col"+ @temp.ToString();

                myDataColumn = new DataColumn();
                myDataColumn.DataType = Type.GetType("System.String");
                myDataColumn.ColumnName = headers[col];
                myInputFile.Columns.Add(myDataColumn);
                ddl_ht.Add(@tempT, headers[col]); 
            }

            // Create new DataRow objects and add to DataTable.    
            for (int r = 0; r < numRows - 1; r++)
            {
                oneRow = data2[r + 1].Split('|');
                myDataRow = myInputFile.NewRow();
                for (int c = 0; c < headers.Length; c++)
                {
                    myDataRow[c] = oneRow[c];
                }
                myInputFile.Rows.Add(myDataRow);
            }
            NumberOfControls = headers.Length;
            myUserSelections = new string[NumberOfControls];
        }

        //Create display panel
        private void CreateDisplayPanel()
        {
            btnSubmit.Style.Add("top", "auto");
            btnSubmit.Style.Add("left", "auto");
            btnSubmit.Style.Add("position", "absolute");
            btnSubmit.Style.Add("top", "200px");
            btnSubmit.Style.Add("left", "400px");

            newcol = CreateDropDownLists();

            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                pnlDisplayData.Controls.Add(newcol[counter]);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
                pnlDisplayData.Visible = true;
                pnlDisplayData.FindControl(newcol[counter].ID);
            }
        }

        //Recreate display panel
        private void RecreateDisplayPanel()
        {
            btnSubmit.Style.Add("top", "auto");
            btnSubmit.Style.Add("left", "auto");
            btnSubmit.Style.Add("position", "absolute");
            btnSubmit.Style.Add("top", "200px");
            btnSubmit.Style.Add("left", "400px");

            newcol = RecreateDropDownLists();

            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                pnlDisplayData.Controls.Add(newcol[counter]);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
                pnlDisplayData.Visible = true;
                pnlDisplayData.FindControl(newcol[counter].ID);
            }
        }

        // Add DropDownList Control to Placeholder
        private DropDownList[] CreateDropDownLists()
        {
            DropDownList[] dropDowns = new DropDownList[NumberOfControls];

            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                DropDownList ddl = new DropDownList();
                SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
                ddl.ID = "DropDownListID" + counter.ToString();

                int NumControls = targettable.Length;
                DataTable dt = new DataTable();
                dt.Load(dr2);

                ddl.DataValueField = "COLUMN_NAME";
                ddl.DataTextField = "COLUMN_NAME";
                ddl.DataSource = dt;
                ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
                ddl.DataBind();

                ddl.AutoPostBack = true;
                ddl.EnableViewState = true; //Preserves View State info on Postbacks
                dr2.Close();
                ddl.Items.Add("IGNORE");

                dropDowns[counter] = ddl;
            }
            return dropDowns;
        }

        protected void ddlList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            string ID = ddl.ID;
        }

        // Add TextBoxes Control to Placeholder
        private DropDownList[] RecreateDropDownLists()
        {
            DropDownList[] dropDowns = new DropDownList[NumberOfControls];

            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                DropDownList ddl = new DropDownList();
                SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);
                ddl.ID = "DropDownListID" + counter.ToString();

                int NumControls = targettable.Length;
                DataTable dt = new DataTable();
                dt.Load(dr2);

                ddl.DataValueField = "COLUMN_NAME";
                ddl.DataTextField = "COLUMN_NAME";
                ddl.DataSource = dt;
                ddl.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
                ddl.DataBind();

                ddl.AutoPostBack = true;
                ddl.EnableViewState = false; //Preserves View State info on Postbacks
                dr2.Close();
                ddl.Items.Add("IGNORE");

                dropDowns[counter] = ddl;
            }
            return dropDowns;
        }

        private void CreateLabels()
        {
            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                Label lbl = new Label();
                lbl.ID = "Label" + counter.ToString();
                lbl.Text = headers[counter];
                lbl.Style["position"] = "absolute";
                lbl.Style["top"] = 60 * counter + 10 + "px";
                lbl.Style["left"] = 250 + "px";

                pnlDisplayData.Controls.Add(lbl);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
            }
        }

        // Add TextBoxes Control to Placeholder
        private void RecreateLabels()
        {
            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                Label lbl = new Label();
                lbl.ID = "Label" + counter.ToString();
                lbl.Text = headers[counter];
                lbl.Style["position"] = "absolute";
                lbl.Style["top"] = 60 * counter + 10 + "px";
                lbl.Style["left"] = 250 + "px";
                pnlDisplayData.Controls.Add(lbl);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
            }
        }

        // Create TextBoxes and DropDownList data here on postback.
        protected override void CreateChildControls()
        {
            // create the child controls if the server control does not contains child controls
            this.EnsureChildControls();

            // Creates a new ControlCollection. 
            this.CreateControlCollection();

            // Here we are recreating controls to persist the ViewState on every post back
            if (Page.IsPostBack)
            {
                RecreateDisplayPanel();
                RecreateLabels();
            }
            // Create these conrols when asp.net page is created
            else
            {
                PopulateFileInputTable();
                CreateDisplayPanel();
                CreateLabels();
            }

            // Prevent dropdownlists and labels from being created again.
            if (restart == false)
            {
                this.ChildControlsCreated = true;
            }
            else if (restart == true)
            {
                this.ChildControlsCreated = false;
            }
        }

        private void AppendRecords()
        {
            switch (targettable)
            {
                case "ContactType":
                    for (int r = 0; r < myInputFile.Rows.Count; r++)
                    { resultLabel.Text = ADONET_methods.AppendDataCT(myInputFile.Rows[r], ddl_ht); }
                    break;
                case "Contact":
                    for (int r = 0; r < myInputFile.Rows.Count; r++)
                    { resultLabel.Text = ADONET_methods.AppendDataC(myInputFile.Rows[r], ddl_ht); }
                    break;
                case "AddressType":
                    for (int r = 0; r < myInputFile.Rows.Count; r++)
                    { resultLabel.Text = ADONET_methods.AppendDataAT(myInputFile.Rows[r], ddl_ht); }
                    break;

                default:
                    resultLabel.Text = "You do not have access to modify this table.  Please select a different target table and try again.";
                    restart = true;
                    break;
                    //throw new ArgumentOutOfRangeException("targettable type", targettable);
            }
        }

        // Read all the data from TextBoxes and DropDownLists 
        protected void btnSubmit_Click(object sender, System.EventArgs e)
        {
            //int cnt = FindOccurence("DropDownListID");
            AppendRecords();
            pnlDisplayData.Visible = false;
            btnSubmit.Visible = false;
            resultLabel.Attributes.Add("style", "align:center");
            btnSubmit.Style.Add("top", "auto");
            btnSubmit.Style.Add("left", "auto");
            btnSubmit.Style.Add("position", "absolute");
            int bSubmitPosition = NumberOfControls;
            btnSubmit.Style.Add("top", System.Convert.ToString(bSubmitPosition)+"px");
            resultLabel.Visible = true;

            Instructions.Visible = false;
            if (restart == true)
            {
                CreateChildControls();
            }
        }

        private int FindOccurence(string substr)
        {
            string reqstr = Request.Form.ToString();
            return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }
        #endregion

    }
//}

© Stack Overflow or respective owner

Related posts about visual-studio-2008

Related posts about c#