I have a form Upload.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Upload Files To DataBase</title>
    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
        <br />
        <asp:Label ID="lblMessage" runat="server" Text="" Font-Names = "Arial"></asp:Label>

Another form Uploadfile.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient ;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing; 

public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)


    private Boolean InsertUpdateData(SqlCommand cmd)
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
            return true;
        catch (Exception ex)
            return false;

    protected void btnUpload_Click(object sender, EventArgs e)
        // Read the file and convert it to Byte Array
        string filePath = FileUpload1.PostedFile.FileName;   
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;

        //Set the contenttype based on File Extension
            case ".doc":
                contenttype = "application/vnd.ms-word";
            case ".docx":
                contenttype = "application/vnd.ms-word";
            case ".xls":
                contenttype = "application/vnd.ms-excel";
            case ".xlsx":
                contenttype = "application/vnd.ms-excel";
            case ".jpg":
                contenttype = "image/jpg";
            case ".png":
                contenttype = "image/png";
            case ".gif":
                contenttype = "image/gif";
            case ".pdf":
                contenttype = "application/pdf";
        if (contenttype != String.Empty)

            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            //insert the file into database
            string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
            lblMessage.ForeColor = System.Drawing.Color.Green;   
            lblMessage.Text = "File Uploaded Successfully";
            lblMessage.ForeColor = System.Drawing.Color.Red;    
            lblMessage.Text = "File format not recognised. Upload Image/Word/PDF/Excel formats";

As it is now its uploaded using internet explorer, I want to be able to upload the files to sqlserver from a windows form within my application. I was trying to convert the Upload.aspx to Upload.cs but was unsuccesful.

Thanks in advance for the help.

There is no simple path to convert an aspx web project to a windows form one.

As an starting point, I will propose the following steps:

  • Create a new CS windows form project. Let the form name as Form1.
  • Design de form to have the 'same' appareince than the web page.
  • Then open the Form1.cs for edit code.
  • You can Copy-Paste from the old code in Uploadfile.aspx.cs (lines 24 to end) to the new file, removing any reference to System.Web and honoring the default code created by the designer on the new project.
  • Adjust the clic events in the forms designer to point to the 'pasted' ones.

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.