Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to loop through the records of the datatable and retrieve data from the each column of the datatable and fill the textboxes.
I want to move through records by a button click.
I have tried this: BUT IT DOESNT WORK.. WHERE IS MY ERROR?

C#
protected void btn_next_Click(object sender, EventArgs e)
       {
           if( inc !=MaxRows-1)
           {
           inc ++;
               navigo();
           }

       }

 protected void Page_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString =" Provider=SQLOLEDB; Data Source=; Initial Catalog=; User ID=; Password=";
            ds = new DataSet();
           
            
             string sql = "SELECT * From Customer";
 
             da = new System.Data.SqlClient.SqlDataAdapter( sql, con );

            con.Open();
            da.Fill( ds, "Customer" );
            navigo();
                 MaxRows = ds.Tables["Customer"].Rows.Count;


            con.Close();
            con.Dispose();
        
        }
        private void navigo()
        {
       
            DataRow dRow = ds.Tables["Customer"].Rows[inc];
            txt_code.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
        txt_name.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
            txt_desc.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
        }


[edit]code block fixed[/edit]
Posted
Updated 24-Nov-12 1:06am
v2
Comments
Nelek 24-Nov-12 7:08am    
Have you tried debugging? Just "it doesn't work" is not so descriptive. What error do you get? You can use the widget "imrpove question" if you need to add / modify any information to your message
Master.Man1980 26-Nov-12 11:58am    
Window or web?
lovitaxxxx 27-Nov-12 4:56am    
web appl

Try this:
C#
foreach(DataRow dr in DataTable)
{
   if(dr[CustomerID].ToString()=="101")
       TextBox1.Text="Customer101";
}
 
Share this answer
 
Comments
lovitaxxxx 25-Nov-12 10:57am    
The problem is that when i increment the value of the inc,it doesnt save the last value i give to it, it remain always 1.. Maybe should i use a session variable to storage the row index??
Quote:
txt_code.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
txt_name.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();
txt_desc.Value = ds.Tables["Customer"].Rows[inc].ItemArray.GetValue(inc).ToString();


I think you should use instead
C#
txt_code.Value = ds.Tables["Customer"].Rows[inc][0].ToString();
txt_name.Value = ds.Tables["Customer"].Rows[inc][1].ToString();
txt_desc.Value = ds.Tables["Customer"].Rows[inc][2].ToString();

where {0,1,2} are the indices of the columns you need (change as appropriate).
 
Share this answer
 
Comments
lovitaxxxx 25-Nov-12 10:56am    
The problem is that when i increment the value of the inc,it doesnt save the last value i give to it, it remain always 1.. Maybe should i use a session variable to storage the row index??
Hello,


I think i can see whats going on,
but i'm not very good at C/Java

So I will try to explain and then give you a sample in VB

Basically your Inc variable is not saving the position because its not got PAGE scope,
Yes you can save it in a session scope variable, but ultimately you DONT want to reload the table everytime you change the row!

So some of your variables need setting at the PAGE scope and others can stay in the subroutine/procedure scope.

its ALWAYS good practice to DECLARE ALL variables as a compulsory step
in visuabl basic we do this by declaring OPTION EXPLICIT at the TOP of the page before ANYTHING else...

And then when you click your NEXT/PREV buttons the routine for the buttons simply pulls the data fromthe dataset which is given page scope.


I dont often use dataset and am more comfortable with an sqlcommand but thats a personal choice and the reason for mentioning this is because im not sure i've coded it right but the code is below and it shouldnt be too difficult to see the differences between my invocation of your code and your invocation.

<script runat="server">

	Dim Increment As Long
	Dim MaxRows As Long
	Dim DataSett As System.Data.DataSet
	

	Protected Sub Page_Load(sender As Object, e As System.EventArgs)
		Dim sqlConn As Data.SqlClient.SqlConnection
		Dim sqlAdapter As Data.SqlClient.SqlDataAdapter
		Dim sqlQuery As String
		'
		sqlConn = New System.Data.SqlClient.SqlConnection()
		sqlConn.ConnectionString = "Provider='SQLOLEDB'; Data Source=; Initial Catalog=; User ID=; Password=;"
		sqlQuery = "SELECT * From Customer"
		sqlAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlQuery, sqlConn)
		sqlConn.Open()
		sqlAdapter.Fill(DataSett, "Customer")
		MaxRows = DataSett.Tables("Customer").Rows.Count
		sqlConn.Close()
		sqlConn.Dispose()
		'tidy up
		sqlConn = Nothing
		sqlAdapter = Nothing
		sqlQuery = Nothing
	End Sub
	Protected Sub Page_Unload(sender As Object, e As System.EventArgs)
		'tidy up
		Increment = Nothing
		MaxRows = Nothing
		DataSett = Nothing
	End Sub
	
		
	Protected Sub btn_prev_Click(sender As Object, e As System.EventArgs)
		If (Increment > 0) Then
			Increment = Increment - 1
			navigo()
		End If
	End Sub
	
	Protected Sub btn_next_Click(sender As Object, e As System.EventArgs)
		If (Increment <> MaxRows - 1) Then
			Increment = Increment + 1
			navigo()
		End If
	End Sub

	Private Sub navigo()
		Dim dRow As System.Data.DataRow
		dRow = DataSett.Tables("Customer").Rows.Item(Increment)
		txt_code.Value = DataSett.Tables("Customer").Rows(Increment).ItemArray.GetValue(Increment).ToString()
		txt_name.Value = DataSett.Tables("Customer").Rows(Increment).ItemArray.GetValue(Increment).ToString()
		txt_desc.Value = DataSett.Tables("Customer").Rows(Increment).ItemArray.GetValue(Increment).ToString()
		'dont forget to tidy up
		dRow = Nothing
	End Sub
		
</script>


Finally, dont forget to tidy up to avoid memory leaks in your web page
 
Share this answer
 
Comments
lovitaxxxx 26-Nov-12 6:50am    
Instead of using session variables
i used a hidden field like this:
<pre> protected void btn_next_Click1(object sender, EventArgs e)
{


if (Hidden_id.Value.Trim() != null)
count = Convert.ToInt32(Hidden_id.Value) + 1;

if (count < MaxRows - 1)
{

navigo();
Hidden_id.Value = count.ToString();


}
} </pre>
But it still doesnt work :/
Well I think i have ported it but im still not sure,

Heres what i came up with....
<script runat="server">

int Inc;
int MaxRows;
System.Data.DataSet	Ds = new System.Data.DataSet();


	void Page_Load(object sender, EventArgs e)
		{
		System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection();
		System.Data.SqlClient.SqlDataAdapter Da = new System.Data.SqlClient.SqlDataAdapter();
		String SqlQuery = "";

			Conn.ConnectionString = "Provider='SQLOLEDB'; Data Source=; Initial Catalog=; User ID=; Password=;";
			SqlQuery = "SELECT * From Customer";
			Da = new System.Data.SqlClient.SqlDataAdapter(SqlQuery, Conn);
			Ds = new System.Data.DataSet();
			Conn.Open();
				Da.Fill(Ds, "Customer");
				MaxRows = Ds.Tables["Customer"].Rows.Count;
			Conn.Close();
			Conn.Dispose();
		}


	protected void btn_prev_Click(object sender, EventArgs e)
		{
			if (Inc > 0)
			{
				Inc--;
				TransferData();
			}
		}


	protected void btn_next_Click(object sender, EventArgs e)
		{
			if (Inc < MaxRows - 1)
			{
				Inc++;
				TransferData();
			}
		}
        

	private void TransferData()
		{
		System.Data.DataRow dRow = Ds.Tables["Customer"].Rows[Inc];
		txt_code.Value = Ds.Tables["Customer"].Rows[Inc].ItemArray.GetValue(0).ToString();
		txt_name.Value = Ds.Tables["Customer"].Rows[Inc].ItemArray.GetValue(1).ToString();
		txt_desc.Value = Ds.Tables["Customer"].Rows[Inc].ItemArray.GetValue(2).ToString();
		}
		
</script>



What confuses me is the semi-colons and squiggly brackets, so you may need to check theyre all in the right place.
 
Share this answer
 

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