Janf Janf - 4 months ago 44
C# Question

Call a method in another class from a CSHTML file

I have searched everywhere but cannot figure out what is wrong here. I have a webb c# app. I have a c# method in the app folder where I query a database and then want to pass that value back to my Cshtml page where I will be makin deciaions based on the value. No matted what I do, when I call the method and then try to read the value I get the error "Cannot implicitly convert type 'class1' to string.

Here is my c#class method and below the calls


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", "Newemail@gibsondunn.com");

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

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

return returnValue;
return "";
catch (Exception err)
throw new ApplicationException(err.Message);



CSHTML Calling

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

ViewBag.Title = "Cash Receipt Sessions test";


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


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

string rights = auth;


Auth throws the error.


Looks like "public static string getdata()" is a static method.

why not try calling it this way:

var auth = class1.getdata();