Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailem C# ASP.NET Class - problém s funkčností třídy

Zdravím, nedávno jsem zde na fóru řešil propojení xls souboru s C#, nyní mám další problém. Nefunguje mi ve třídě tento kód (Ukazje error: ,,The name 'Server' does not exist in the current context" v connection stringu Server.MapPath):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace Objekt
{
    public class nazev
    {
        public static string getText()
        {
            String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +           Server.MapPath("data.xls") + ";Extended Properties=\"Excel 8.0;HDR=No\"";
            OleDbConnection objConn = new OleDbConnection(sConnectionString);

            objConn.Open();

            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [List1$B2:B2]", objConn);
            object nazev = objCmdSelect.ExecuteScalar();
            return nazev.ToString();

            objConn.Close();
        }
    }
           
}

Potřebuji aby to následně fungovalo tak, abych si to mohl klasicky v kódu vyčíst tímto způsobem: this.Label1.Text = Objekt.nazev.getText();

Předmět Autor Datum
using System.Web;
Wikan 18.04.2012 19:16
Wikan
Kdepak, stále to samé
Isair 18.04.2012 19:22
Isair
Classes you add will not have any connection to the HttpContext, i.e they will not have access to th…
Wikan 18.04.2012 19:27
Wikan
nahradil jsem to tímto: String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="…
Isair 18.04.2012 19:42
Isair
A musíš tu cestu zjišťovat přes Server.MapPath?
Wikan 18.04.2012 19:47
Wikan
pokud nechám jen data.xls ta to sice projde, ale ten soubor to nenajde i když je přímo u projektu
Isair 18.04.2012 19:49
Isair
A proč jsi to dal celé do závorky?
Wikan 18.04.2012 19:50
Wikan
i bez závorky to nejde, chová se to i bez ní stejně...
Isair 18.04.2012 19:51
Isair
Jakou chybu to přesně hlásí?
Wikan 18.04.2012 19:52
Wikan
Tak ted to nechápu, ale něco jsem omylem smáznul a začlo to makat... Zde je funkční kód: using Syst… poslední
Isair 18.04.2012 19:56
Isair

Classes you add will not have any connection to the HttpContext, i.e they will not have access to things like Request, Response, Session and Server.
If the method is called when a request is in progress, you will be able to access Server using

HttpContext.Current.Server

A better approach though is that you add the target folder as a method argument and create the path from that. That way, the class will be usable in non-web contexts as well.
public void CreateFile(string[] PR,string targetfolder)

1

nahradil jsem to tímto:

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + (HttpContext.Current.Server.MapPath ("data.xls")) + ";Extended Properties=\"Excel 8.0;HDR=No\"";
            OleDbConnection objConn = new OleDbConnection(sConnectionString);

...a stále nefunguje, tohle hází chybu cannot be used like a method...

Tak ted to nechápu, ale něco jsem omylem smáznul a začlo to makat... Zde je funkční kód:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Web;

namespace Objekt
{
    public class test
    {
        public static string getText()
        {
            /* String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("data.xls") + ";Extended Properties=\"Excel 8.0;HDR=No\"";
            OleDbConnection objConn = new OleDbConnection(sConnectionString); */
            String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + (HttpContext.Current.Server.MapPath ("data.xls")) + ";Extended Properties=\"Excel 8.0;HDR=No\"";
            OleDbConnection objConn = new OleDbConnection(sConnectionString);

            objConn.Open();

            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [List1$B2:B2]", objConn);
            object nazev = objCmdSelect.ExecuteScalar();
            return nazev.ToString();

            objConn.Close();
        }
    }
           
}

Zpět do poradny Odpovědět na původní otázku Nahoru