tags:

views:

252

answers:

3

Hello. 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();
+7  A: 

You need to return the DataTable at the end of your function:

return myDT;
TLiebe
thanks for the comment. but by doing this, i have a new error.Cannot implicitly convert type 'System.Data.DataTable' to 'string'
Nana
Can you provide your updated code?
TLiebe
okies. i have edited it. it is at the *** return myDT; ***
Nana
The first line of your code in Display.aspx.cs is causing the problem. MergeTA() returns a DataTable and you're trying to assign it to a string variable. MergeTA() is NOT returning a connection string - you need to get the connection string from somewhere else (your app.config or web.config file, manually create it or whatever).
TLiebe
oh okies. thanks. i'll go try.
Nana
+3  A: 

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

return(myDT);
Jerry Bullard
D'oh - beat me by one second! I knew I shouldn't have spent time proof reading.
TLiebe
Woah, is that return statement formatted like a function call? Weieeerrrrd....
Robert Fraser
TLiebe, for once in ten tries I finally got one in first! Still you currently have more votes, probably due to my "weieeerrrd" parens. :-)Robert, yeah, I'm weird like that. Return just feels like something that needs an argument. I have no idea how I picked up this habit, but back in the 20th century I must have used a C compiler that wanted it.
Jerry Bullard
thanks for the comment. but by doing this, i have a new error.Cannot implicitly convert type 'System.Data.DataTable' to 'string'
Nana
Yeah, I've had other almost identical answers beat mine before too. At least I can help you out with a +1.
TLiebe
TLiebe, thanks, and a +1 back for your graciousness and for answering the follow-up. Wish I could +2.
Jerry Bullard
Ah, I can actually do that extra +1 with a comment. There you go.
Jerry Bullard
A: 

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.

JoshJordan