how create a Function return as DataTable with C# ?
-
my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }
-
my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }
-
my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }
Additionally, the following line is pointless:
xingselex wrote:
DataTable Dt = new DataTable();
Since you assign it later on to:
xingselex wrote:
Dt = Ds.Tables["_Objects"];
This means you're creating an instance of a DataTable that is then discarded without use. Instead just declare it, or explicitly assign to null:
DataTable Dt;
or
DataTable Dt = null;
10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011
-
my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }
I think u just remove the last 2 lines. Then after
Cn.Close();
writereturn Dt;