Yasmine Amin

Yasmine Amin

  • NA
  • 20
  • 0

Data binding in ASP .NET 1.1 with C#

Jan 27 2009 12:26 PM

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

<asp:DataList ID = "userList" Runat="server">

      <ItemTemplate>  

                Username List: <%#Eval("username")%>

      </ItemTemplate> 

</asp:DataList>  

Now to my aspx.cs page

private void Page_Load(object sender, System.EventArgs e)

{

//Create an object to access class DbConn

DbConn 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 connection

connect.Open();

//execute the command and store the results into readVals

SqlDataReader 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 

 


Answers (1)