Wednesday, April 27, 2016

MS Access connection with ASP.NET C# / Gridview bind with Access / Remove HTML tags

Introduction:

Here I will explain the below programming

1 )How to make connection with MS Access  in ASP.NET
2) Date bind gridview with Dataset from MS Access.
3) Remove HTML tags from the database filed.
(It will removed all HTML tags from the database filed and show as text only in Grid.)


Function HTML tags remove

public string HTMLToText2(string HTMLCode)
{
    // Getting Input HTML
    string inputHtml = HTMLCode;

    // Removing HTML tags including   from the input
    string outputText = StripHtmlTags(inputHtml);
    // Assigning plain text output to output textbox
    HTMLCode = outputText;
   
    return HTMLCode;
}


public string StripHtmlTags(string source)
{
    return Regex.Replace(source, "<.*?>|&.*?;", string.Empty);
}


Function MS Access DB connection

protected void btnExcursionLoad_Click(object sender, EventArgs e)
{
    string ConnectString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("alfa.mdb;");
    OleDbConnection objCon;
    OleDbCommand objCmd;
    DataSet Ds = new DataSet();

    string GetSql = string.Empty;
    GetSql = "(SELECT * FROM Excursions)";
    using (objCon = new OleDbConnection(ConnectString))
    {
        using (objCmd = new OleDbCommand(GetSql, objCon))
        {
            OleDbDataAdapter Da = new OleDbDataAdapter(objCmd);
            Da.Fill(Ds);
            GridView2.DataSource = Ds;
            GridView2.DataBind();
        }
    }
}

The blow code is aspx.cs code behind file.
C# Code
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.Data;
using System.Data.OleDb;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Text;
protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        // upload file code Save into Server folder >> Filed //
        if (FileUpload1.HasFile)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            string FolderPath = ConfigurationManager.AppSettings["FolderPath"];

            string FilePath = Server.MapPath(FolderPath + FileName);
            FileUpload1.SaveAs(FilePath);
            Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);
        }
    }
 
    // -- import Excel connection -- //
    private void Import_To_Grid(string FilePath, string Extension, string isHDR)
    {
        string conStr = "";
        switch (Extension)
        {
            case ".xls": //Excel 97-03
                conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                break;
            case ".xlsx": //Excel 07
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                break;
        }
        conStr = String.Format(conStr, FilePath, isHDR);
        OleDbConnection connExcel = new OleDbConnection(conStr);
        OleDbCommand cmdExcel = new OleDbCommand();
        OleDbDataAdapter oda = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        cmdExcel.Connection = connExcel;

        //Get the name of First Sheet
        connExcel.Open();
        DataTable dtExcelSchema;
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
        connExcel.Close();

        //Read Data from First Sheet
        connExcel.Open();
        cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
        oda.SelectCommand = cmdExcel;
        oda.Fill(dt);
        connExcel.Close();

        //Bind Data to GridView
        GridView1.Caption = Path.GetFileName(FilePath);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    public string StripHtmlTags(string source)
    {
        return Regex.Replace(source, "<.*?>|&.*?;", string.Empty);
    }
    public string HTMLToText2(string HTMLCode)
    {
        // Getting Input HTML
        string inputHtml = HTMLCode;

        // Removing HTML tags including   from the input
        string outputText = StripHtmlTags(inputHtml);
        // Assigning plain text output to output textbox
        HTMLCode = outputText;
      
        return HTMLCode;
    }


   // --- Data Gried pageing doe ---//
    protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
        string FileName = GridView1.Caption;
        string Extension = Path.GetExtension(FileName);
        string FilePath = Server.MapPath(FolderPath + FileName);

        Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    // --- MS Access DB connection --//
    protected void btnExcursionLoad_Click(object sender, EventArgs e)
    {
        string ConnectString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("alfa.mdb;");
        OleDbConnection objCon;
        OleDbCommand objCmd;
        DataSet Ds = new DataSet();

        string GetSql = string.Empty;
        GetSql = "(SELECT * FROM Excursions)";
        using (objCon = new OleDbConnection(ConnectString))
        {
            using (objCmd = new OleDbCommand(GetSql, objCon))
            {
                OleDbDataAdapter Da = new OleDbDataAdapter(objCmd);
                Da.Fill(Ds);
                GridView2.DataSource = Ds;
                GridView2.DataBind();
            }
        }
    }

Download Code 

No comments:

Post a Comment