Form is not working with backend code

Posted on 2011-09-19
Last Modified: 2012-05-12
IN changing the file with your suggestions I now get an error on the frontend.
Not sure how to handle this: It looks like i need to name the labels but when I try it gives me even more errors...Stuck.

Parser Error Message: '' is not a valid identifier.

Source Error:

Line 6:        <body MS_POSITIONING="GridLayout">
Line 7:              <form id="Form1" method="post" runat="server" enctype="multipart/form-data">
Line 8:                          <asp:label id="" runat="server" Font-Bold="True">Please Select the Data File you will be uploading:</asp:label>
Line 9:                          <input id="filMyFile" type="file" runat="server">
Line 10:                         <p></p>

<%@ Page language="c#" AutoEventWireUp="false" Codebehind="default.aspx.cs" Inherits="FileUpload.WebForm1" Src="default.aspx.cs" %>

	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
				<asp:label id="" runat="server" Font-Bold="True">Please Select the Data File you will be uploading:</asp:label>
				<input id="filMyFile" type="file" runat="server">
                <asp:label id="lblnotes1" runat="server" Font-Bold="True">Please Select the Data File you will be uploading:</asp:label>
				<input id="lblnotes1" type="Text" runat="server">
				<asp:button id="cmdSend" runat="server" Text="Send"/>
				<asp:Label id="lblInfo" runat="server" Font-Bold="True" Visible="false"></asp:Label>
								<asp:Label id="lblText1" runat="server" Font-Bold="True" Visible="True">This file will be placed in the FSU folder on the CEDB server.</asp:Label>
								<asp:Label id="lblText2" runat="server" Font-Bold="True" Visible="True">This file and all of its meta data will be stored in uploadedData database</asp:Label>
								<asp:Image id="imgFile" runat="server" Visible="False"></asp:Image>
								<asp:Image id="imgDB" runat="server" Visible="False"></asp:Image>

Open in new window

Question by:Tagom
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5

Expert Comment

ID: 36562692
<asp:label id=""  

needs to have an identifier.

Expert Comment

ID: 36562698
And if you're not using anything in code behind, it doesn't matter, you can just call it

<asp:label id="label1" ....

and it'll be fine.

Expert Comment

ID: 36562701
(line 8, btw)
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Author Comment

ID: 36563069
I did try this method
When I fill in the names of the label I get the following error
Parser Error Message: The base class includes the field 'lblnotes1', but its type (System.Web.UI.WebControls.Label) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlInputText).

Source Error:

Line 10:                         <p></p>
Line 11:                 <asp:label id="lblnotes" runat="server" Font-Bold="True">Please Select the Data File you will be uploading:</asp:label>
Line 12:                         <input id="lblnotes1" type="Text" runat="server">
Line 13:                         <asp:button id="cmdSend" runat="server" Text="Send"/>
Line 14:                         <p></p>

Source File: /hgdeposition/DataUpload/default.aspx    Line: 12

In the database the column notes is a text field -
I am not sure what else to call this input field on the form. So I am wondering if I have line 20 coded incorrectly in the backend file
Here is the back code
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;

namespace FileUpload

    public class WebForm1 : System.Web.UI.Page
        private const string MDBFILE = "FileUpload.mdb";

        protected Label lblFile;
        protected HtmlInputFile filMyFile;
        protected System.Web.UI.WebControls.Label lblInfo;
        protected System.Web.UI.WebControls.Label lblnotes1;
        protected System.Web.UI.WebControls.Button cmdSend;
        protected System.Web.UI.WebControls.Image imgFile;
        protected System.Web.UI.WebControls.Image imgDB;
        protected System.Web.UI.WebControls.Label lblText1;
        protected System.Web.UI.WebControls.Label lblText2;

        override protected void OnInit(EventArgs e)

        private void InitializeComponent()
            this.Load += new System.EventHandler(this.Page_Load);
            this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);

        private void Page_Load(object sender, System.EventArgs e)
            // Check if FileID was passed to this page as a parameter
            if (Request.QueryString["FileID"] != null)
                // Get the file out of database and return it to requesting client


        // Processes click on our cmdSend button
        private void cmdSend_Click(object sender, System.EventArgs e)
			// Check to see if file was uploaded
			if( filMyFile.PostedFile != null )
				// Get a reference to PostedFile object
				HttpPostedFile myFile = filMyFile.PostedFile;

				// Get size of uploaded file
				int nFileLen = myFile.ContentLength; 

				// make sure the size of the file is > 0
				if( nFileLen > 0 )
					// Allocate a buffer for reading of the file
					byte[] myData = new byte[nFileLen];

					// Read uploaded file from the Stream
                    myFile.InputStream.Read(myData, 0, nFileLen);

					// Create a name for the file to store
					string strFilename = Path.GetFileName(myFile.FileName);

					// Write data into a file
                    WriteToFile(Server.MapPath(@"FSU\" + strFilename), ref myData);

					// Store it in database
                    int nFileID = WriteToDB(strFilename, myFile.ContentType, ref myData, lblnotes1.Text);

					// Set label's text
					lblInfo.Text = 
						"Filename: " + strFilename + "<br>" + 
						"Size: " + nFileLen.ToString() + "<br>";

					// Set URL of the the object to point to the file we've just saved
					imgFile.ImageUrl = strFilename;
					imgFile.ToolTip = "This file will be stored to the FSU folder as file.";
					lblText1.Text = imgFile.ImageUrl;

					// Set URL of the the object to point to the this script with ID of the file
					// that will retrieve file out the database
					imgDB.ImageUrl = GetMyName() + "?FileID=" + nFileID.ToString();
					imgDB.ToolTip = "This file Will be stored in the uploadData database.";
					lblText2.Text = imgDB.ImageUrl;
                    // show the images and text
					imgFile.Visible = false;
					imgDB.Visible = false;
					lblText1.Visible = true;
					lblText2.Visible = true;

        // Writes file to current folder
        private void WriteToFile(string strPath, ref byte[] Buffer)
            // Create a file
            FileStream newFile = new FileStream(strPath, FileMode.Create);

            // Write data to the file
            newFile.Write(Buffer, 0, Buffer.Length);

            // Close file

        // Generates database connection string
        private string GetConnectionString()
            return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(MDBFILE) + ";";

        // Writes file to the database
        private int WriteToDB(string strName, string strType, ref byte[] Buffer, string strNotes)
            int nFileID = 0;

            // Create connection
            OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

            // Create Adapter
            OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT * FROM tblFile", dbConn);

            // We need this to get an ID back from the database
            dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            // Create and initialize CommandBuilder
            OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt);

            // Open Connection

            // New DataSet
            DataSet dbSet = new DataSet();

            // Populate DataSet with data
            dbAdapt.Fill(dbSet, "tblFile");

            // Get reference to our table
            DataTable dbTable = dbSet.Tables["tblFile"];

            // Create new row
            DataRow dbRow = dbTable.NewRow();

            // Store data in the row
            dbRow["FileName"] = strName;
            dbRow["FileSize"] = Buffer.Length;
            dbRow["ContentType"] = strType;
            dbRow["FileData"] = Buffer;
            dbRow["uploadDate"] = DateTime.Now;
            dbRow["Notes"] = strNotes;

            // Add row back to table

            // Update data source
            dbAdapt.Update(dbSet, "tblFile");

            // Get newFileID
            if (!dbRow.IsNull("FileID"))
                nFileID = (int)dbRow["FileID"];

            // Close connection

            // Return FileID
            return nFileID;

        // Read file out of the database and returns it to client
        private void ShowTheFile(int FileID)
            // Define SQL select statement
            string SQL = "SELECT FileSize, FileData, ContentType, Notes FROM tblFile WHERE FileID = "
                        + FileID.ToString();

            // Create Connection object
            OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

            // Create Command Object
            OleDbCommand dbComm = new OleDbCommand(SQL, dbConn);

            // Open Connection

            // Execute command and receive DataReader
            OleDbDataReader dbRead = dbComm.ExecuteReader();

            // Read row

            // Clear Response buffer

            // Set ContentType to the ContentType of our file
            Response.ContentType = (string)dbRead["ContentType"];

            // Write data out of database into Output Stream
            Response.OutputStream.Write((byte[])dbRead["FileData"], 0, (int)dbRead["FileSize"]);

            // Close database connection

            // End the page

        // Reads the name of current web page
        private string GetMyName()
            // Get the script name
            string strScript = Request.ServerVariables["SCRIPT_NAME"];

            // Get position of last slash
            int nPos = strScript.LastIndexOf("/");

            // Get everything after slash
            if (nPos > -1)
                strScript = strScript.Substring(nPos + 1);

            return strScript;

Open in new window


Expert Comment

ID: 36563162
protected System.Web.UI.WebControls.Label lblnotes1;

needs to be

protected System.Web.UI.HtmlControls.HtmlInputText lblnotes1;


Author Comment

ID: 36563442
do you by chance have a link with a good summary of input control types

Expert Comment

ID: 36563468
Yeah, I verified it here:

You can also intellisense / navigate it in your code behind, just type
protected System.Web.UI.HtmlControls.
and the dot at the end, and that'll bring up the list of items in there.

Author Comment

ID: 36563474
now i get this error - been chasing this down all morning! appreciate your help

Compiler Error Message: CS0117: 'System.Web.UI.HtmlControls.HtmlInputText' does not contain a definition for 'Text'

Source Error:

Line 78:
Line 79:                               // Store it in database
Line 80:                     int nFileID = WriteToDB(strFilename, myFile.ContentType, ref myData, lblnotes1.Text);
Line 81:
Line 82:                               // Set label's text

Expert Comment

ID: 36563484
Also, remember there is a difference in ASP.NET input types, and the classic HTML input types. Whenever possible, you want to use the ASP.NET input types (<asp:TextBox for instance), as they provide more flexibility and functionality. There's also <asp:FileUpload which works pretty well.


Accepted Solution

NauticalNonsense earned 500 total points
ID: 36563502
hahhahaha, we were typing at the same time...
asp:TextBox has a .Text on it, I believe that your HtmlInputText is a .Value
Yeah, it's .Value for that control.

Author Closing Comment

ID: 36563609
It has been a long time since I have had to program in .asp of any kind!
Works....Thank you so much!
So I could use a good tutorial for
The next step is adding a Boolean column  to the database so they can keep up with if file is complete :
I need to add a checkbox to the form and the backEnd.

Expert Comment

ID: 36563940
You're very welcome, thanks for the grade!

Your checkbox should be mostly straightforward, you'll use
<asp:CheckBox id="ShowInNewWindow" (or whatever it is,

and then on the back side you can reference it like this:

if (ShowInNewWindow.Checked)
{ do stuff }
{ do other stuff }

Or bind that ShowInNewWindow.Checked to any boolean directly

 dbRow["ShowInNewWindow"] = ShowInNewWindow.Checked;


Author Comment

ID: 36564049
I am going to open a new case as this is not working and it will give you the points.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question