1

I've tried for a while now and I really dont get it. I recive error "Cannot implicitly convert type 'void' to 'string'" I have tried multiple variants of string, int, nothing, void, public, static and nope I really dont get it right.

I want to get one value from my db thoug my DAL and BLL, my code looks like this.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Repeater1.DataSource = BLL.getGames();
        Repeater1.DataBind();
        var culture = CultureInfo.GetCultureInfo("sv-SE");
        var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);
        var dateTime = DateTime.Today;
        int weekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
        string mroundY = dateTime.Year.ToString();
        string mroundW = weekNumber.ToString();
        string mrounddate = mroundY + mroundW;

        string mround = BLL.getMroundGames(mrounddate);  <-- Here's the error

    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
}

My BLL looks like this;

    public class BLL
{
    public static void getMroundGames(string mrounddate)
    {
        SqlCommand getMroundGames = new SqlCommand("SELECT mround FROM gameTB where mround = @mrounddate");
        DAL.ExecuteNonQuery(getMroundGames);
     }
}

Also tried this;

public class BLL
{
    public static DataTable getMroundGames(string mrounddate)
    {
        SqlCommand getMroundGames = new SqlCommand("SELECT mround FROM gameTB where mround = @mrounddate");
        getMroundGames.Parameters.Add("@mrounddate", SqlDbType.VarChar, 10).Value = mrounddate;
        return DAL.GetData(getMroundGames);
    }
}

And finaly my DAL looks like this;

public class DAL
    {
    public static SqlConnection GetConnection()
        {
            SqlConnection conn = new
            SqlConnection(ConfigurationManager.ConnectionStrings["tiptopConnectionString"].ConnectionString);
            conn.Open();
            return conn;
        }

    public static DataTable GetData(SqlCommand command)
    {
        try
        {
            using (SqlConnection conn = GetConnection())
            {
                using (DataSet ds = new DataSet())
                {
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        da.SelectCommand = command;
                        da.SelectCommand.Connection = conn;
                        da.Fill(ds);
                        return ds.Tables[0];
                    }
                }
            }
        }
        catch (Exception err)
        {
            throw new ApplicationException(string.Format("Felmeddelande:{0}", err.Message));
        }
    }

    public static object ExecuteScalar(SqlCommand command)
    {
        using (SqlConnection conn = GetConnection())
        {
            command.Connection = conn;
            object result = command.ExecuteScalar();
            return result;
        }
    }

    public static void ExecuteNonQuery(SqlCommand command)
    {
        using (SqlConnection conn = GetConnection())
        {
            command.Connection = conn;
            command.ExecuteNonQuery();
        }
    }

}

Where to begin?

Steve
  • 213,761
  • 22
  • 232
  • 286
Slint
  • 355
  • 1
  • 4
  • 17
  • 6
    Your function is `void` ie returns nothing. And you try to assign that to a string... What do you expect / intend to happen? – Floris Mar 06 '13 at 23:33
  • Why are you returning void? – Saher Ahwal Mar 06 '13 at 23:33
  • Ok instead of making things harder. How would you do a simple as posible query for a single value to present on your aspx page where mround = current year current week of year. So the sql would be; Select mround from gameTB where mround = 201310 And i get the sql value to my label with id mroundlabel? – Slint Mar 07 '13 at 00:10

2 Answers2

10

The signature for this is wrong;

public static void getMroundGames(string mrounddate)

You need to change it to something similar to ;

public static string getMroundGames(string mrounddate)

Retrieve the string value from your DAL and return to the consumer accordingly.

var dt = Dal.GetData();
return (string)dt.Rows[0]["field"];

However, in all honesty, I would not be passing a datatable from your DAL to your BLL. I would return the string directly or introduce a DTO and populate this from your DAL through your BLL, back to the consumer.

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
2

You need to add a return type of string to getMroundGameas(string mrounddate).

It's not clear what type of object DAL is, but you should also probably use ExecuteReader method, http://msdn.microsoft.com/en-us/library/bb344397.aspx rather then ExecuteNonQuery. This will return a Reader which can be queried for the value returned by the select statement.

Finally you should return the value.

alanh
  • 1,153
  • 10
  • 14