This my first question on this site..I have been studying c# for a year or so..
I have created a class called Student withe property: Name, age , Studentid,...
Then I have created an overloaded method as a list
Public list GetAllStudent(string sGetStudentName_in)
{
Student OGetStudentName = new Student();
}
Could any one give a guidance as how to complete this method and return the Name( s) only..
I omitted the connection to the database as I have retrieved the Student as an object and send the information to a DataTable as a result.
Please any help or similar example will help as I'm still trying to find my way around c# programming .
Thank you in advance
Jignesh TrivediPosted Oct 15, 2013, 12:45 AM
here, you have to assgin one by on property to your object..
List oCustomerList = new List();
using (DBProvider.DGenericSQL oSQLQueryDAL = new DBProvider.DGenericSQL(conSQLConnectionString))
{
string sSQLQuery = string.Format( "Select top 200 * from student");
System.Data.DataTable oDTResults = oSQLQueryDAL.ExecuteSQL(sSQLQuery, System.Data.CommandType.Text);
foreach (System.Data.DataRow oDataRow in oDTResults.Rows)
{
var oCustomer = new ContactDetails();
oCustomer.Name = Convert.ToString(oDataRow["Name']);
//...... Assign all column .....
//now add it to list....
oCustomerList.Add(oCustomer);
}
}
return oCustomerList
hope this will help you.
Riddhi ValechaPosted Oct 15, 2013, 3:50 AM
Share your project naa.... I will debug and find the error..
Abbas HamzaPosted Oct 14, 2013, 1:20 PM
i have created the method to be called when a search button clicked and a search box is empty so I need to bring all the details of the Student then pass it to a method which I'll call later to be linked to a list view.
my code as follow:
public static List
{
List
try
{
using (DBProvider.DGenericSQL oSQLQueryDAL = new DBProvider.DGenericSQL(conSQLConnectionString))
{
string sSQLQuery = string.Format( "Select top 200 * from student");
System.Data.DataTable oDTResults = oSQLQueryDAL.ExecuteSQL(sSQLQuery, System.Data.CommandType.Text);
foreach (System.Data.DataRow oDataRow in oDTResults.Rows)
{
// I initiate the Contact class here
ContactDetails oContact detail = ContactDetails();
...."............ Here I want to pass oDTResults data ....".".................
oCustomerList
}
}
}
catch (Exception oError)
{
//ToDo: Handle Exception;
}
//ToDo: i need to return the object
// when I debug the return oCcustomerList it is always empy
return oCustomerList;
}
Riddhi ValechaPosted Oct 13, 2013, 7:10 AM
Instead of List, use DataTable of System.Data namespace.
I am giving you my logic-
--
public DataTable ReturnStudentsName()
{
try
{
if(Connection Is Open)
{
cmd = new SQLCommand();
cmd.connection = con;
string s = "select studentName from Students";
cmd.commandtext = s;
adp = new sqldataadapter();
adp.selectcommand = cmd;
table = new datatable();
adp.fill(table);
if(table.rows.count > 0)
return table
else
return null;
}
}
catch(Exception err){err.Message.ToString(); return null;}
}