Here’s the error:
An object reference is required for a non-static field, method, or
property ‘ArchiCapture.Models.dbConnection.runSproc(string[],
string[], string)’
It appears at the call dt = dbConnection.runSproc(paramName, paramValue, “pr_select_employee_by_id”);
I’m trying to develop a class that will call SPROCs. Here’s my class.
namespace ArchiCapture.Models
{
public class dbConnection
{
public DataTable runSproc(string[] paramName, string[] paramValue, string sproc)
{
SqlConnection conn = null;
DataTable dt = new DataTable();
SqlDataReader reader = null;
try
{
string connStr = ConfigurationManager.ConnectionStrings["AC2012"].ConnectionString;
using (conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand(sproc, conn);
cmd.CommandType = CommandType.StoredProcedure;
for (int x = 0; x < paramName.Count(); x++)
{
cmd.Parameters.Add(new SqlParameter(paramName[x], paramValue[x]));
}
conn.Open();
reader = cmd.ExecuteReader();
dt.Load(reader);
return dt;
}
}
catch (SqlException e)
{
throw (e);
}
catch (Exception ex)
{
throw (ex);
}
finally
{
if (conn != null)
{
conn.Close();
}
if (reader != null)
{
reader.Close();
}
}
}
}
}
And here’s how I’m calling it..
DataTable dt = new DataTable();
string[] paramName = new string[1] { "@employee_id" };
string[] paramValue = new string[1] { searchEngine.Value };
dt = dbConnection.runSproc(paramName, paramValue, "pr_select_employee_by_id");
You want a
staticmethod:Or, as the error message states, you must instantiate a new instance of your class before calling an instance method:
Also, generally class names are PascalCased. Consider
DbConnectioninstead ofdbConnection.