2016-11-08 78 views
-1

我已经遍寻搜索,但无法弄清楚这里有什么问题。我有一个webb c#应用程序。我有一个c#方法在我查询数据库的应用程序文件夹中,然后想要将该值传递回我的Cshtml页面,在那里我将根据值进行makin deciaions。不纠结我做什么,当我打电话的方法,然后尝试读取我的错误“无法键入‘1类’隐式转换为字符串值。从CSHTML文件中调用另一个类的方法

这里是我的C#类的方法和下面的电话

方法:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public class class1 

    { 
    public static string getdata() 
    { 

     string cnnString = ConfigurationManager.ConnectionStrings["GDC_WellsFargoConnectionString"].ConnectionString; 
     SqlDataReader reader; 
     string returnValue; 



     /// var cmd = "insert into Email Insert values(@name,@email"; 
     string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
     using (SqlConnection cnn = new SqlConnection(cnnString)) 
     { 

      using (SqlCommand cmd = new SqlCommand("Select isnull([Authorization],'none') as authoriztion from PNSWebAuthorization where username = '" + userName + "'", cnn)) 
      { 
       ////cmd.Parameters.AddWithValue("@name", "Newname,First"); 
       //// cmd.Parameters.AddWithValue("@email", "[email protected]"); 

       ///string authcode; 
       cnn.Open(); 
       try { 
     reader = cmd.ExecuteReader(); 

        if (reader.Read()) 
        { 
         returnValue = reader["Authorization"].ToString(); 
         reader.Close(); 


         return returnValue; 
        } 
        else 
        { 
         reader.Close(); 
         return ""; 
        } 
       } 
       catch (Exception err) 
       { 
        throw new ApplicationException(err.Message); 
       } 
       finally 
       { 

        cnn.Close(); 
        cnn.Dispose(); 
       } 


      } 
     } 
    } 



CSHTML Calling 

@model GDC.Finance.WebClient.Areas.Treasury.ViewModels.CashReceiptSessionListViewModel 
@using PagedList.Mvc; 


@{ 
    ViewBag.Title = "Cash Receipt Sessions test"; 
} 

<h2>Sessions</h2> 

@section scripts{ 
    <script src="~/Scripts/popup.js"></script> 

} 
@{ 

    /// string auth = "none"; 
    var auth = new class1(); 
    class1.getdata(); 

    string rights = auth; 



} 

Auth throws the error. 

回答

0

看起来像 “公共静态字符串的GetData()” 是一个静态方法

为什么不尝试调用这种方式:

var auth = class1.getdata(); 
+0

我会试一试 – Janf

+0

这个工作完全谢谢你 – Janf

相关问题