简体   繁体   中英

Datatable error in c#

I used a datatable for fetching some data from mysql db,my error is

not all code paths return a value

ie return DS.Tables[0] is unable to access inside a loop or if statement.How can i solve this issue?

public DataTable GetAlltheatredet()//I got error here//
{
    DataTable myalltheat = GetAllmytheatdet();
    foreach (DataRow drow1 in myalltheat.Rows)
    {
        usermsterid = drow1["UserMasterId"].ToString();
        if (usrmid == usermsterid)
        {
            flag = 1;
            break;
        }
        if (flag == 1)
        {
            try
            {
                string connString = "Server=localhost;database=Mytable;uid=root;";
                string query = "SELECT * FROM `Mytable`.`Mydata`";
                MySqlDataAdapter ma = new MySqlDataAdapter(query, connString);
                DataSet DS = new DataSet();
                ma.Fill(DS);
                return DS.Tables[0];
            }
            catch (MySqlException e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}

you have if(flag==1) { ... return ... } you need to add an else statement with either a return or a throw new ...

public DataTable GetAlltheatredet()//I got error here//
{
    DataTable myalltheat = GetAllmytheatdet();
    foreach (DataRow drow1 in myalltheat.Rows)
    {
        usermsterid = drow1["UserMasterId"].ToString();
        if (usrmid == usermsterid)
        {
            flag = 1;
            break;
        }
        if (flag == 1)
        {
            try
            {
                string connString = "Server=localhost;database=Mytable;uid=root;";
                string query = "SELECT * FROM `Mytable`.`Mydata`";
                MySqlDataAdapter ma = new MySqlDataAdapter(query, connString);
                DataSet DS = new DataSet();
                ma.Fill(DS);
                return DS.Tables[0];
            }
            catch (MySqlException e)
            {
                throw new Exception(e.Message);
            }
        }
        else {
            throw new Exception("Flag isn't 1 and I don't know what to do");
        }
    }
}

这是一个众所周知的错误,如果返回DataType的方法具有任何多路径块,例如if {} else {}或switch case,则应在它们的每个路径中返回一些内容。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM