I have a webform that is pulling data from a SQL Server Table. I want to add a button to the form, and when this button is clicked it take the user to the next record in the database (and by next I mean the increment up one ID...if they are on record 100
when the buttonis pressed go to record 101). Then the exact opposite for another button, when this button is pressed decrement one record by id. What would the syntax be to complete this operation?
You can already do this easily with a gridview and set the paging and record count to 1, so the buttons will do this easily for you. If you create your custom method to read the records, you can create a counter variable to read each data item incrementally
or decrement the record based on the direction.
Check out my website: http://www.TheTradeBox.com
Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
You can make use of asp Table control and and hidden fileld for your requirement. Set intial value of the hidden field to zero & when ever button is clicked increment the hidden fileld value and get the record based on the hidden field value like
SELECT * FROM Table_Name WHERE Primay_Key='"Int.Parse(HiddenField.value)"'
You can make use of asp Table control and and hidden fileld for your requirement. Set intial value of the hidden field to zero & when ever button is clicked increment the hidden fileld value and get the record based on the hidden field value like
SELECT * FROM Table_Name WHERE Primay_Key='"Int.Parse(HiddenField.value)"'
I like the way you are thinking, I am just running into one snag. I today had to update my SQL Statement to add in a WHERE condition to say WHERE onsite = 'YES' so it will no longer be just a simple iteration through all id's in the table, it will be an
iteration through id's where onsite = 'Yes'. How would that change the example you presented?
No big deal....:) just add the WHERE clause to the query like
SELECT * FROM Table_Name WHERE Primay_Key='"Int.Parse(HiddenField.value)"' AND onsite='YES';
Okay I ran into one more ? --- how do I stop at the end of records? For example, if I decremented to 1 if I hit previous one more time it will show a -1, -2 etc etc. even tho there are no records with that ID. Or if I hit next record let's say last ID is
10201 if I hit next record it will go to 10202 even tho there is no record with that ID. How can I stop when there are no more records (whether it be incrementing or decrementing?)
Fill the datatable with the query and check if their is row or not like
//DataTable dt= new DataTable();
//dt= fill your Data Table
if(dt.rows.count>0)
{
//display the records
}
else
{
//Display a Message saying that -No record found
}
Fill the datatable with the query and check if their is row or not like
//DataTable dt= new DataTable();
//dt= fill your Data Table
if(dt.rows.count>0)
{
//display the records
}
else
{
//Display a Message saying that -No record found
}
I am actually using DataReader
SqlDataReader dr = cmd.ExecuteReader();
if (dr.FieldCount["ID"].ToString() > 0)
But when I try that syntax I get a compile erorr of can no apply indexing with [] to an expression type of 'int'
IndigoMontoy...
Member
16 Points
51 Posts
Going through records in Database
Feb 21, 2013 08:31 PM|LINK
I have a webform that is pulling data from a SQL Server Table. I want to add a button to the form, and when this button is clicked it take the user to the next record in the database (and by next I mean the increment up one ID...if they are on record 100 when the buttonis pressed go to record 101). Then the exact opposite for another button, when this button is pressed decrement one record by id. What would the syntax be to complete this operation?
mebinici
Participant
829 Points
258 Posts
Re: Going through records in Database
Feb 21, 2013 09:06 PM|LINK
There are many was to do this.
You can already do this easily with a gridview and set the paging and record count to 1, so the buttons will do this easily for you. If you create your custom method to read the records, you can create a counter variable to read each data item incrementally or decrement the record based on the direction.
Love collecting video games, movies and board games!
Enjoying my '11 WRX, so sexy...
IndigoMontoy...
Member
16 Points
51 Posts
Re: Going through records in Database
Feb 21, 2013 10:05 PM|LINK
I would prefer to not use a datagrid if that is possible, or at least not a datagrid that is visible for the users to see.
Do you have an example (or know of an already existent one) that shows how to create a method to handle this?
Pbalan.in
Contributor
2144 Points
484 Posts
Re: Going through records in Database
Feb 22, 2013 03:34 AM|LINK
Hi using the rownumber option you can view this next record...
Declare @ChildTable Table(RefNo nvarchar(50),Typ nvarchar(50),Qty numeric(18,0));
Insert into @ChildTable Values ('001','A',2300);
Insert into @ChildTable Values ('002','B',1000);
Insert into @ChildTable Values ('005','C',1500);
Insert into @ChildTable Values ('006','D',3800);
Insert into @ChildTable Values ('007','C',500);
Insert into @ChildTable Values ('008','C',100);
Select * from (
Select ROW_NUMBER() OVER (ORDER BY RefNo) Rownum,*
from @ChildTable) DerivedTable where Rownum = 4;
Mark as answer if it useful
raghavendra ...
Participant
1890 Points
435 Posts
Re: Going through records in Database
Feb 22, 2013 03:45 AM|LINK
You can make use of asp Table control and and hidden fileld for your requirement. Set intial value of the hidden field to zero & when ever button is clicked increment the hidden fileld value and get the record based on the hidden field value like
SELECT * FROM Table_Name WHERE Primay_Key='"Int.Parse(HiddenField.value)"'
IndigoMontoy...
Member
16 Points
51 Posts
Re: Going through records in Database
Feb 22, 2013 02:11 PM|LINK
I like the way you are thinking, I am just running into one snag. I today had to update my SQL Statement to add in a WHERE condition to say WHERE onsite = 'YES' so it will no longer be just a simple iteration through all id's in the table, it will be an iteration through id's where onsite = 'Yes'. How would that change the example you presented?
raghavendra ...
Participant
1890 Points
435 Posts
Re: Going through records in Database
Feb 23, 2013 06:16 AM|LINK
No big deal....:) just add the WHERE clause to the query like
SELECT * FROM Table_Name WHERE Primay_Key='"Int.Parse(HiddenField.value)"' AND onsite='YES';
IndigoMontoy...
Member
16 Points
51 Posts
Re: Going through records in Database
Feb 23, 2013 09:23 PM|LINK
raghavendra ...
Participant
1890 Points
435 Posts
Re: Going through records in Database
Feb 24, 2013 04:49 AM|LINK
Fill the datatable with the query and check if their is row or not like
//DataTable dt= new DataTable(); //dt= fill your Data Table if(dt.rows.count>0) { //display the records } else { //Display a Message saying that -No record found }IndigoMontoy...
Member
16 Points
51 Posts
Re: Going through records in Database
Feb 24, 2013 06:02 PM|LINK
I am actually using DataReader