0

Hello again,

I have a method:

public static void GetStudents(DropDownList list, Label lbl)
    {
        SqlConnection conn = new SqlConnection(Config.DbConnectionString);
        SqlCommand cmd = new SqlCommand("GetStudentsToDDL", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader reader;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ListItem item = new ListItem();
                item.Text = reader["StudentName"].ToString() + " " + reader["StudentFamilyname"].ToString();
                item.Value = reader["StudentID"].ToString();
                list.Items.Add(item);
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            lbl.Text = ex.Message;
            Utilities.LogError(ex);
            throw;
        }
        finally
        {
            conn.Close();
        }
    }
ddlStudents.DataSource = CatalogAccess.GetStudents(ddlStudents, lbl);
ddlStudents.DataBind();

Says: Error 3 Cannot implicitly convert type 'void' to 'object'

Where's the problem?

Edited by Nfurman: n/a

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Nfurman
0
public static void GetStudents ...

Your method returns nothing (void) yet your other code expects it to be returning something.

0
public static void GetStudents ...

Your method returns nothing (void) yet your other code expects it to be returning something.

OK. I know what the 'void' means. I just don't understand what I should return? This method just suppose to connect to DB and populate dropdownlist.

0

You don't have to pass anything back. You are already adding to the dropdown list in the code, so setting the datasource isn't needed. Get rid of the assignment:

CatalogAccess.GetStudents(ddlStudents, lbl);

should be all you need.

0

this approach doesn't work for me. I've improved the method:

public void GetStudents()
    {
        SqlConnection conn = new SqlConnection(Config.DbConnectionString);
        SqlCommand cmd = new SqlCommand("GetStudentsToDDL", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetStudentsToDDL";
        SqlDataReader reader;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                DropDownList ddlStudents=new DropDownList();
                ListItem item = new ListItem();
                item.Text = reader["StudentName"].ToString() + " " + reader["StudentFamilyname"].ToString();
                item.Value = reader["StudentID"].ToString();
                ddlStudents.Items.Add(item);
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            Label lbl=new Label();
            lbl.Text = ex.Message;
            Utilities.LogError(ex);
            throw;
        }
        finally
        {
            conn.Close();
        }
    }

And the code in WebUserControl

CatalogAccess ca = new CatalogAccess();
        ddlStudents.DataSource = ca.GetStudents();
        ddlStudents.DataBind();

I am still getting the Error Cannot implicitly convert type 'void' to 'object'

0

Hi again. I've rewritten my method and now it returns DataTable

public static DataTable GetStudents(DropDownList ddlStudents, Label lbl)
    {
        SqlConnection conn = new SqlConnection(Config.DbConnectionString);
        SqlCommand cmd = new SqlCommand("GetStudentsToDDL", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetStudentsToDDL";
        DataTable tbl = new DataTable();
        SqlDataReader reader;
        
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            
            while (reader.Read())
            {
                 ListItem item = new ListItem();
                 item.Text = reader["StudentName"].ToString()+" " + reader["StudentFamilyname"].ToString();
                item.Value = reader["StudentID"].ToString();
                ddlStudents.Items.Add(item);
                tbl.Load(reader);
            }
            
            reader.Close();
        }
        catch (Exception ex)
        {
            lbl.Text = ex.Message;
            Utilities.LogError(ex);
            throw;
        }
        finally
        {
            conn.Close();
        }
        return tbl;
    }

Another error: System.IndexOutOfRangeException: StudentName

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.