Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
the currently method is returing back a DataTable, und it should return back a class. its like a Task but why, what is different ? and how it could be look like ?

the normal code is doing the following :'look what have you tried'.

so now i would like to do some method but return back a class with properties

What I have tried:

public static DataTable GetSend(string IP, string User)
{
string sSql "some query with join";
if (User != string.Empty)
{
sSQL += " and CrtUser = '" + User + "'";

}
and the same with IP
and the end
System.Diagnostics.Debug.WriteLine(sSQL);

SqlConnection con = getSQL_Connection();

return GetDataTable(ref con, sSQL);
}
Posted
Updated 2-Oct-19 2:12am
v2
Comments
F-ES Sitecore 2-Oct-19 6:42am    
What do you mean by "class"? Technically DataTable *is* a class.
[no name] 2-Oct-19 6:45am    
i mean instead of public static DateTable GetSend .. i would create a class for example public static Send GetSend then i fill some properties insaid the class .
Afzaal Ahmad Zeeshan 2-Oct-19 6:51am    
Does it return Task<datatable>? Your question is a lot confusing as it stands.
[no name] 2-Oct-19 7:04am    
yes it returns datatable back but i want to do class with properties then return the class back ... make it sence now !
CHill60 2-Oct-19 7:20am    
You need to use the "Reply" link if you want to respond to a comment - otherwise the member is not notified of your response

1 solution

While it's not impossible - or even difficult to return a class instead of a DataTable, it's not something that we can help you with, and that you need to think about fairly carefully.

First off: A DataTable isn't a single item of data, while the class you describe is:
Quote:
i want to do class with properties then return the class back

A class is easy:
C#
public class MyClass
   {
   public int ID { get; set; }
   public string UserName { get; set; }
   public string UserType  { get; set; }
   }
Create an instance in your method, fill out the details, and return it.

But a DataTable contains data about many rows: so you probably don't want your method to return "a class", but a Collection of classes: perhaps a List<MyClass>

Think about what exactly you are trying to do, and what your DataTable currently contains, and it should be pretty trivial for you to construct the appropriate class, and return it or a collection of it.

But we can't do that for you!



Quote:
the normal code is doing the following :
public static DataTable GetSend(string IP, string User)
{
string sSql "some query with join";
if (User != string.Empty)
{
sSQL += " and CrtUser = '" + User + "'";

}

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And that code is useless to us: we still have no idea what is in the DataTable, so we still can't help you.
 
Share this answer
 
v2
Comments
[no name] 2-Oct-19 7:59am    
the normal code is doing the following :
public static DataTable GetSend(string IP, string User)
{
string sSql "some query with join";
if (User != string.Empty)
{
sSQL += " and CrtUser = '" + User + "'";

}
and the same with IP
and the end
System.Diagnostics.Debug.WriteLine(sSQL);

SqlConnection con = getSQL_Connection();

return GetDataTable(ref con, sSQL);
}

so now i would like to do some method but return back a class with properties
BillWoodruff 2-Oct-19 8:06am    
Please edit your original post, and add this information.
[no name] 2-Oct-19 8:12am    
i did
OriginalGriff 2-Oct-19 8:15am    
Answer updated.
[no name] 2-Oct-19 8:34am    
Thank for help Paul and explanation.. the Code is not from me and i am trying to refactory the code . do you prefer to use EF ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900