Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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?
 
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 24-Nov-12 2:05am
Edited 24-Nov-12 2:06am
Nelek62.8K
v2
Comments
Nelek at 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 at 26-Nov-12 11:58am
   
Window or web?
Annnaa at 27-Nov-12 4:56am
   
web appl
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
foreach(DataRow dr in DataTable)
{
   if(dr[CustomerID].ToString()=="101")
       TextBox1.Text="Customer101";
}
  Permalink  
Comments
Annnaa at 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??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
    
  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).
  Permalink  
Comments
Annnaa at 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??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Comments
Annnaa at 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 :/
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100