Hi all
I'm having a problem with my code. All i'm trying to do is select some values from a DB and disply them on my asp page.
I'm using DataList to display the values.
Also what i'm trying to is have one class that does my DB connection (so when my project grow i ll be able to avoid me rewriting the whole connect to DB part of the code).
So here is my ASPX page
Username List: <%#Eval("username")%>
Now to my aspx.cs page
private
void Page_Load(object sender, System.EventArgs e){
//Create an object to access class DbConnDbConn connect =
new DbConn();connect.connectToDb();
}
-------Function to diplay the values
<% #Eval("username");%> public void foundUsers (string retrievedData){
userList.DataSource = retrievedData;
userList.DataBind();
}
And now to my connect to db class public void connectToDb(){
string conn = ConfigurationSettings.AppSettings["theConn"];SqlConnection connect =
new SqlConnection(conn); string theCmd = "SELECT username FROM _USERS";SqlCommand cmd =
new SqlCommand(theCmd,connect); //open up the connectionconnect.Open();
//execute the command and store the results into readValsSqlDataReader readVals = cmd.ExecuteReader();
string theVals = readVals.ToString();//create an object to access getData
getData vals =
new getData();userList.DataSource = readVals;
connect.Close(); //Close connection
vals.foundUsers(theVals); //return the values to foundUsers function
}
Now i think the problem is with passing back string values rather the db data types but i'm not sure how to fix this problem. can any one help
Thanks in advance
Kalyan VedantamPosted Jan 27, 2009, 1:47 PM
Hi Yasmine,
I dont think you can use a string as a datasource. So in your connectToDb method after you get the datareader,open it and read the values and add them to an arraylist or a list object.Then pass the arraylist or list object to the foundusers method as a parameter and bind that to the datalist.
--declare arraylist
while(readVals.Read())
{
add users to arraylist
}
--close reader
founduser(arraylist)