Solutions: Tough asp.net Code + Workarounds (C#)

Nemeses

Well-Known Member
Joined
Jul 15, 2010
Messages
223
Reaction score
1
Location
Durban
Hey guys,

Herein lies personal code snippets and work-arounds to some tricky asp.net situations.
Please note that I am not some super-awesome coder, and that I will make a few mistakes. I am open to suggestive, and some constructive criticism.
Also, please make requests on what you need done, and I will try make a plan to get there!

I will keep this as updated as possible.​


CSS : Embedded Fonts
Code:
@font-face {
  font-family: 'Summerti'; /* we can name this whatever we want */
  src: url(Summerti.eot);       /* IE will download this anyway */ 
  src: local('Summertime'),       /* It'll ignore this.. */
         url(Fonts/Summerti.tff) format('truetype');
}

h1, h2, h3 {
	margin: 0;
	font-weight: normal
	color: #1E3F7F;
        font-family: Summerti;
}
Explanation:
@font-face will only work in CSS3

[font-family] Will give the class a header, to call whenever you want.

[src:url] This is for Internet Explorer users. You need to give the relative (or direct) URL to the font.
Font MUST be in oet format. Unfortunatly, IE users will have this font downloaded to their PC for it to be used.
This might override any previous font with that name.

[src:local] IE will ignore this. Give the full NAME (NOT File name) of the font)
URL to the font, and [format] of w...om/downloads/connector/net/5.1.html#downloads
--> Change it to the latest version, and get the MSI installer. Then, copy the required .dll's from the install directory to your project folder. Reference those files.

I put the Connect & Disconnect in different classes, so you only need to call the class eg. ConnectToDatabase();, instead of re-writing.


web.config: ".Net Framework Data Provider. It may not be installed." FIX
Code:
 <system.data>
   <DbProviderFactories>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
Explanation:
On my system, I never needed this (And it caused an error) when running it from VS2010. This fix is only for site hosts.


web.config: Just add this
Code:
  <system.web>
    <customErrors mode="Off"/>
    <httpRuntime requestValidationMode="2.0" />
    <pages validateRequest="false" />
</system.web
Explanation:
I'm writing this tutorial at 10:03am.. Ill remember why you need this later.


mix : Adding a new Content page, linking to a Master
This is a very arb way of doing it. Will be happy for recommendations on easier ways

Create 2 .txt files
Page.txt
HTML:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body" Runat="Server">
</asp:Content>

CodeBehind.txt
HTML:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
}

Code:
private void CreatePage()
    {
        string pageName = txtPageName.Text.Replace("'", "&#39");
        string m = Server.MapPath("../");
        string fileLoc = m + pageName + ".aspx";
        string filecLoc = m + pageName + ".aspx.cs";

        FileStream fs = new FileStream(fileLoc, FileMode.Create);
        BufferedStream bs = new BufferedStream(fs);
        fs.Close();

        FileStream fs1 = new FileStream(filecLoc, FileMode.Create);
        BufferedStream bs1 = new BufferedStream(fs1);
        fs1.Close();

        //Get data for page

        //Get data for Page
        StreamReader pageRead = new StreamReader(m + "Page.txt");
        string page = pageRead.ReadToEnd();
        pageRead.Close();
        page = page.Replace("Default2.aspx.cs", pageName+".aspx.cs");

        //Populate Page
        StreamWriter sw = new StreamWriter(fileLoc);
        sw.Write(@page);
        sw.Close();

        //Get data for Code Behind
        StreamReader codeRead = new StreamReader(m + "CodeBehind.txt");
        string code = codeRead.ReadToEnd();
        codeRead.Close();

        //Populate Code Behind
        StreamWriter swcb = new StreamWriter(filecLoc);
        swcb.Write(@code);
        swcb.Close();

    }

Code:
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //Create Page method needs to be called here for some reason.
        CreatePage();
        //Make sure that you have a connection ready to insert Data
        ConnectToDatabase();

        //This makes sure that you are able to use ', and replaces .aspx with nothing.
        string pageName = txtPageName.Text.Replace("'", "&#39");
        pageName = pageName.Replace(".aspx", "");
        string title = txtTitle.Text.Replace("'", "&#39");
        string data = txtData.Text.Replace("'", "&#39");

        try
        {
           //This will make sure the page is added to the database, to be referenced
           MySqlCommand cmd = sqlConn.CreateCommand();
            cmd.CommandText = @"INSERT INTO pages (heading, data, name) VALUES ('"+@pageName+".aspx','"+@data+"','"+@title+"')";
            cmd.ExecuteNonQuery();
            DisconnectDatabase();
            //Refresh the page
            Response.Redirect(Request.Url.AbsoluteUri);
            
        }
        catch (MySqlException em)
        {
            txtData.Text = em.ToString();

        }
    }

Explanation:
After many diverse ways, the best way I could make a new file, was to manually create a file called [whatever].aspx and [whatever].aspx.cs.
Then, I would copy code out of the 2 text files, and insert it into the 2 newly created files, changing the linked name.

After that, all I needed to do was make sure that the page was referenced in the data base, and voila! Done :)
 
Last edited:
Add this to the sticky, I can update the contents for you as well.

Edit:
Otherwise if there's a lot of talk about this, I can just link to this thread.
 
Let me finish the entire thing, and we can decide. Either way, knowledge is knowledge.
 
I find it very disappointing, I am with MWEB, and I can't use the .net MySQL connector to connect to the mySQL database, hell not even mySQL Workbench will connect.

Will I have better luck if I use MS SQL rather than mySQL?
 
For VS2010, MsSQL is favoured. There are nice (default ) connection strings etc. So that is a good option.
Unfortunately, most sites do not offer MsSQL support, or its an added extra, through payment.

When you download the .msi from my link, and install it, go to your 'C:\Program Files\MySQL\MySQL Connector Net 6.3.7'

From there, proceed to copy the
MySQL.VisualStudio.dll
MySql.Data.dll
MySql.Data.Entity.dll
MySql.Web.dll
to your websites .bin directory.

Then, go to Website > Add Referenct, Click on the browse tab, and select all of the .dll's.

Then, add
using MySql.Data.MySqlClient;
to the top of your project.

That should fix all problems.
 
I find it very disappointing, I am with MWEB, and I can't use the .net MySQL connector to connect to the mySQL database, hell not even mySQL Workbench will connect.

Will I have better luck if I use MS SQL rather than mySQL?

I can't find much information on Mweb's hosting options on their site. Are you using the unlimited hosting? Isn't that PHP?
 
I can't find much information on Mweb's hosting options on their site. Are you using the unlimited hosting? Isn't that PHP?

You get an option to choose Microsoft (no php as far as I know) or Linux ( no asp.net I am assuming)

For VS2010, MsSQL is favoured. There are nice (default ) connection strings etc. So that is a good option.
Unfortunately, most sites do not offer MsSQL support, or its an added extra, through payment.
<snip>

Thanks, going to try it now.

Few things I have noticed:
a) there is no bin folder :( ( I'll manually create one)
b)
Code:
Then, go to Website > Add Referenct, Click on the browse tab, and select all of the .dll's.
THis is not possible, MWeb has no real control panel.
 
Last edited:
a) there is no bin folder :( ( I'll manually create one)

No no.
In the solution explorer, right click on the project (Underneath 'Solution [Name]),
Go down to 'Add ASP.NET Folder',
and click on Bin.

This will make all files in there secure, and properly reference-able.
 
Top
Sign up to the MyBroadband newsletter
X