简体   繁体   中英

Error CS0161: Not all code paths return a value

I have this error. Can anyone tell me why? In TA.cs

public class TA
{
    public TA()
    {
    }

    public static DataTable MergeTA()
    {

        DataTable myDT = new DataTable();
        myDataTable.Columns.Add("AcadYear", typeof(string));
        myDataTable.Columns.Add("NofGrp", typeof(System.Int16));
        myDataTable.Columns.Add("LecHr", typeof(int));
        ...
        ...
        ...

        DataRow myDR = myDT.NewRow();
        myDataRow["AcadYear"] = "2009";
        myDataRow["NoofGrp"] = "2";
        myDataRow["LecHr"] = "1";
        ...
        ...
        ...

        myDT.Rows.Add(myDR);
        ***return myDT;*** 

    }

}

In Display.aspx.cs

...
...
...
string strConMethod = TA.MergeTA();
        SqlConnection sqlConMethod = new SqlConnection(strConMethod);
        DataTable haha = new DataTable();
        haha = TA.MergeTA();

您需要在函数结束时返回DataTable:

return myDT;

You need to return a DataTable from your MergeTA method. Add this to the bottom:

return(myDT);

You appear not to be returning myDT at the end of MergeTA().

That method is of type DataTable , so all code paths through it must return a DataTable .

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