quest
-
hi am trying to do display the items from database to label.when i click the NEXT button ineed to display the second record to the label . how its posssible
Get the items in a datatable. Maintain a counter variable to count the previous and next clicks. According to the counter variable display values from datatable. Keep datatable in one session or cache.
printf("Navaneeth!!") www.w3hearts.com
-
hi am trying to do display the items from database to label.when i click the NEXT button ineed to display the second record to the label . how its posssible
Suppose you have the required records in a datatable dt, you only need to iterate the row index keeping the column index constant. The rows index value needs to be stored into a hidden field after each iteration. Below is the C# code for the default.aspx for displaying the records.
using System; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { Cls_DBtier Cls_DBtierobj = new Cls_DBtier(); int i = 0; DataTable dt; protected void Page_Load(object sender, EventArgs e) { Cls_DBtierobj.mvar_DBConnectionstring = Application["Connection_String"].ToString(); Cls_DBtierobj.OpenConnection(); dt = Cls_DBtierobj.getNames(); i = dt.Rows.Count; Cls_DBtierobj.CloseConnection(); LblNames.Text = dt.Rows[0][0].ToString(); } protected void BtnNxt_Click(object sender, EventArgs e) { int index = Convert.ToInt32(HidIndex.Value); if (index < i-1) { index = index + 1; HidIndex.Value = index.ToString(); LblNames.Text = dt.Rows[index][0].ToString(); } } }
HidIndex is the hidden field, LblNames is the label for displaying the records. And BtnNxt is the button display the next record. Cls_DBtier is class file. Below is the code for it.using System; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; public class Cls_DBtier { public string mvar_DBConnectionstring; SqlConnection conn; public void OpenConnection() { conn = new SqlConnection(mvar_DBConnectionstring); } public void CloseConnection() { conn.Close(); } public SqlCommand ReturnSqlCommand(string sqlStmt) { SqlCommand Cmd; try { Cmd = new SqlCommand(); Cmd.Connection = conn; Cmd.CommandType = CommandType.Text; Cmd.CommandText = sqlStmt; return Cmd; } finally { Cmd = null; } } public DataTable getNames() { string stt = "SELECT ename FROM employee"; SqlDataAdapter SAdaptor; SqlCommand Cmd; DataSet ds=new DataSet(); DataTable dt; try { Cmd=ReturnSqlC