How to select n rows using IDataReader - asp.net

[ASP .Net - Microsoft Visual Web Developer 2010]
Hi all,
I've problem with this code:
With MenuNavCatDataSource
Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)
If xReader.Read Then
MenuNavCat1.Text = xReader.Item("MenuCategoryName")
MenuNavCat2.Text = xReader.Item("MenuCategoryName")
MenuNavCat3.Text = xReader.Item("MenuCategoryName")
MenuNavCat4.Text = xReader.Item("MenuCategoryName")
MenuNavCat5.Text = xReader.Item("MenuCategoryName")
End If
End With
I've 5 label and I want to parse the content of the label from the database. The database contains menus ordered from 1 to 5. And I want to show it on:
- 'MenuNavCat1' label, menu with order number 1 on database,
- 'MenuNavCat2' label, menu with order number 2 on database, and so on...
How to add where statement to the code, just like 'WHERE OrderNo = 1', and so on..?
Need your help guys..
Thank you so much.
EDIT
Here is the database:
Table MenuNavCategory
| Column Name | Data Type |
|-----------------------------|
| MenuNavCatID | int |
| CategoryName | varchar(20) |
| OrderNumber | int |
|-----------------------------|
And there is some value inserted to the table.
I want to show the 'CategoryName' onto some label.
For example:
lblCat1 | lblCat2 | lblCat3 | lblCat4
Then, using that xReader.Read, store the value on the database onto that label based on the OrderNumber...

The DataReader only reads one row of data at a time. If you have 5 labels then you should have 5 rows in your database. Then you iterate through each row using the DataReader and assign the labels:
With MenuNavCatDataSource
Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)
'i is just a counter to keep track of what row we are on.
Dim i as integer = 1
If xReader.Read Then
Select Case i
Case 1 'First Row from DB.
MenuNavCat1.Text = xReader.Item("MenuCategoryName")
Case 2 'Second Row from DB... etc.
MenuNavCat2.Text = xReader.Item("MenuCategoryName")
Case 3
MenuNavCat3.Text = xReader.Item("MenuCategoryName")
Case 4
MenuNavCat4.Text = xReader.Item("MenuCategoryName")
Case 5
MenuNavCat5.Text = xReader.Item("MenuCategoryName")
x+=1
End If
End With
The above is just an example to try and explain how the DataReader works. I'd find another way to actually code it as the above relies on the rows in the database being in the correct order and is pretty ugly.

Related

Printing SSRS report to label printer x times

I am sorry if this is a duplicate question and please point me to the answer if it is.
Here is my situation:
I have an ASP.NET web forms site that uses SQL server database as its data source. The end user wants to print labels to a Zebra label printer. (Old printer Zebra 110XiIIIPlus-200dpi) I can install this printer on the end users system or it can run from the web server, doesn't matter it is a network printer.
I can retrieve the data from the database ok. My problem starts when I need to print. Lets say that I have four parts, p1 p2 p3 & p4. All the labels have the same format:
Job #, Mark #, Customer, Width(in), Length(in) (which all come from the SQL DB)
The only field that is pulled in query and not printed is the qty. Mark # is the part number (Don't know why it isn't just called part #). Now lets say that p1 has a qty of 12, p2 has a qty of 25, p3 has a qty 321, and p4 has a qty of 35.
When it prints I need to send 12 "copies" of the label for p1, 25 "copies" for p2, 321 "copies for p3, and 35 "copies" for p4.
How do I send 12 labels for p1 to be printed, then use the next record's data and send 24 labels etc. etc.?
I do not have any code for printing yet because i can not figure out how to do this!! Does anyone know of a way that I can do it.
I did find one article here on SO: Print a report Multiple times, (SSRS reporting services) but I am not sure how to make it work, if it even can) for what I need.
One last note I am using VB.Net in the code behind if it makes a difference.
Any help is very much appreciated!
I had to do the exact same thing, the solution I came up with was to loop though the select and union the same select by the number of items in the Quantity. By doing this you should get 12 rows for the P1 since that that is the quantity of boxes, all the data should be the same except for Page# that should auto increase by 1 until the end of the quantity.
Results would be something like:
Job# | Mark# | Quantity | Page
------------------------------
1 | P1 | 12 | 1
1 | P1 | 12 | 2
1 | P1 | 12 | 3
1 | P1 | 12 | 4
.....
1 | P1 | 12 | 12
Then you would group on Mark# and Page and Create a Page break between each instance of a group, this will make it so you get the number of pages based on the quantity.
Thanks to the help of SO user newGuy I was able to figure out how to do this. Here is the solution that I cam up with that works.
--Drop Temp tables if exists
If OBJECT_ID('tempdb..#Labels') Is Not Null Drop Table #Labels
If OBJECT_ID('tempdb..#Pieces') Is Not Null Drop Table #Pieces
--Declare variables
Declare #MarkNumber varchar(10)
Declare #Qty int
Declare #RowCount int = 0
Create Table #Labels
(
vjobnum varchar(12),
marknumber varchar(25),
customer varchar(25),
pwidth decimal(18,4),
plength decimal(18,4)
)
Create Table #Pieces
(
marknum varchar(25),
totqty int,
customer varchar(50),
jobnum varchar(12),
plength decimal(18,4),
pwidth decimal(18,4)
)
Insert Into #Pieces(marknum, totqty, customer, jobnum, plength, pwidth)
Select od.marknum, od.qty, oh.customer, oh.van_job_num, od.bbin, od.cbin From tbl_order_detail od Join tbl_order_head oh On oh.ordernum = od.ordernum Where od.ordernum = (Select Distinct ordernum From tbl_BearingBarRpt)
Set #RowCount = (Select COUNT(*) From #Pieces)
While #RowCount > 0 --Exists (Select marknum From #piecelabels)
Begin
Select #MarkNumber = (Select a.marknum From (Select ROW_NUMBER() OVER (Order By marknum) as RowNumbers, *From #Pieces) a Where a.RowNumbers = #RowCount)
Select #Qty = (Select totqty From #Pieces Where marknum = #MarkNumber)
While #Qty > 0
Begin
Insert Into #Labels(vjobnum, marknumber, customer, pwidth, plength)
Select pc.jobnum, pc.marknum, pc.customer, pwidth, plength
From #Pieces pc
Where pc.marknum = #MarkNumber
--Decrement the Qty counter
Set #Qty = #Qty - 1
End
Set #RowCount = #RowCount - 1
End
It may not be the best but it definitely works!

Select Case, when no data return

it is possible do a SELECT CASE, decode, nvl or another query function when I need verify if the return of a select query is empty or has a value?
For example, I have this:
Record | type | id_customer
-------+--------+-------------
1 | T | cus1
2 | A | cus2
3 | T | cus3
4 | | cus4
If I do this:
select decode(type,'T','Main','A','Adicional','none') from table where record=1;
I get Main.
If I fo this:
select decode(type,'T','Main','A','Adicional','none') from table where record=4;
I get none.
But if I do this:
select decode(type,'T','Main','A','Aditional','none') from table where record=5;
I get nothing, and is logic. So, I need get the decode value when the row exist and a text if the rows no exist.
So, I tried with SELECT CASE but is not posible get a value using COUNT. For example like this:
SELECT
CASE
WHEN count(1)>0 THEN decode(type,'T','Main','A','Aditional','none')
ELSE '-'
END
FROM TABLE WHERE record=5;
And get a ' - ', or the same if the record is 2, get 'Aditional'
Thanks a lot.
You can use aggregate functions min or max outside expression:
select max(decode(type,'T','Main','A','Aditional','none'))
from table
where record=5;
If query returns one row, you get value of that row. If query returns 0 rows, you get NULL.
Then you can replace NULL using nvl:
select nvl(max(decode(type,'T','Main','A','Aditional','none')), ' - ')
from table
where record=5;
EDIT
Also, if you need to choose one string from several:
select decode(max(decode(type,'T', 2, 'A', 1, 0)), 0, 'none', 1, 'Additional', 2, 'Main', null, ' - ')
from table
where record=5;
This is an option:
select decode(type,'T','Main','A','Aditional','none')
from table
where record = 5
union all
select '-'
from dual
where not exists (select 1 from table where record = 5);
It selects records with record = 5 and unifies them with '-', if no records exits with record = 5. Check out this Fiddle.

highlight gridview cell by header and row values

I have a gridview and I was able to highlight a cell based on field name and criteria of cell in that column. Now I want to do the same thing but also consider the row value from the second column as well. Example below
Gender | Part Number | Small | Medium | Large
Men | C-888-TVN | 5 | 6 | 9
Men | C-777-TV4 | 7 | 7 | 8
So if I want to highlight the '7' in the C-777-TV4 row and under the Medium size how would I go about doing both criteria. This is not based on a number value criteria just the name of the header "Medium" and string value in the part number row "C-777-TV4".
Here is the new code that i'm getting an out of range error:
Protected Sub gridStock_HtmlRowPrepared(sender As Object, e As ASPxGridViewTableRowEventArgs) Handles gridStock.HtmlRowPrepared
If e.RowType <> GridViewRowType.Data Then
Return
End If
Dim name As String = e.GetValue("CorePN").ToString()
If name = "777-M-MBL" Then
e.Row.Cells(10).BackColor = System.Drawing.Color.Red
End If
End Sub
Getting the error at e.Row.Cells(10).Backcolor
I need to see more code of yours. Here is what I thought:
If row.Cell(1).Text = "C-777-TV4" Then
e.Row.Cells(5).BackColor = System.Drawing.Color.Cyan
End If

GridView : Bind Multiple value from database in single column

I want data in gridview like this:
CategoryName Subcategory Name
--------------------------------
Abc Abc1,abc2,abc3
Bcs Bcs1,bcs2
def Null / No Record
How can I do that?
okay i want data from database by using single table. i have one table categories in which i have field like categryid, parentid,name. when parentid is 0 then its known by Categories else all other is subCategories.
I Am using asp.net with c# and i want to do this in gridview with using boundfield. for categories i have done but for sub categories i dont have any idea how to do.
subcategories is idedntify by its parentid. in sub categories parentid =categryid
Assuming data in your table is something like this
CategoryId| ParentID| Name |
1 | 0 | A |,
2 | 1 | B |,
3 | 0 | C |,
4 | 3 | D |
You can do something like
Create Table #ReportTable(Id identity int,CategoryId int,Category varchar(10),SubCategory Varchar(10))
Declare #CountOfRecords int = Select count(categoryid) from categories
Declare #TableIterator int = 1
While #TableIterator <= #CountOfRecords
Begin
Declare #ParentId int = (Select ParentId From Categories Where CategoryId=#TableIterator)
If #ParentId = 0
Begin
Insert Into #ReportTable(CategoryId,Category)
Select CategoryId,Category
From Categories
Where CategoryId = #TableIterator
End
Else
Begin
Update #ReportTable
Set SubCategory =
(Select SubCategory From Categories Where CategoryId = #TableIterator)
And Id = #ParentId
End
Set #TableIterator = #TableIterator + 1
End

SQLite Compare two columns

I am creating a database for my Psych class and I am scoring a personality profile. I need to compare two test items and, if they match a condition, then copy into a separate table.
Example (pseudocode is between \)Sqlite3
INSERT INTO Scale
SELECT* FROM Questions
WHERE \\if Question 1 IS 'TRUE' AND Question 3 IS 'FALSE' THEN Copy this Question
and its response into the Scale table\\;
I have about 100 other questions that work like this. Sample format goes like this:
IF FirstQuestion IS value AND SecondQuestion IS value THEN
Copy both questions into the Scale TABLE.
---------- EDITED AFTER FIRST RESPONSE! EDITS FOLLOW-------------
Here is my TestItems table:
ItemID | ItemQuestion | ItemResponse
```````````````````````````````````````````````````
1 | Is the sky blue? | TRUE
2 | Are you a person? | TRUE
3 | 2 Plus 2 Equals Five | FALSE
What I want to do: If Question 1 is TRUE AND Question 3 is FALSE, then insert BOTH questions into the table 'Scale' (which is setup like TestItems). I tried this:
INSERT INTO Scale
SELECT * FROM TestItems
WHERE ((ItemID=1) AND (ItemResponse='TRUE'))
AND ((ItemID=3) AND (ItemResponse='FALSE'));
HOWEVER: The above INSERT copies neither.
The Resulting 'Scale' table should look like this:
ItemID | ItemQuestion | ItemResponse
```````````````````````````````````````````````````
1 | Is the sky blue? | TRUE
3 | 2 Plus 2 Equals Five | FALSE
There is nothing wrong with your query. You're just there:
INSERT INTO Scale
SELECT * FROM Questions
WHERE `Question 1` = 1 AND `Question 3` = 0;
Here 1 and 0 are values (in your first case, true and false). First of all you should ensure there are fields Question 1 and Question 3 in your Questions table. Secondly the column count as well as data types of Scale table should match Questions table. Otherwise you will have to do selectively choose the fields in your SELECT query.
Edit: To respond to your edit, I am not seeing an elegant solution. You could do this:
INSERT INTO Scale
SELECT * FROM TestItems WHERE ItemID = 1 AND ItemResponse = 'TRUE'
UNION
SELECT * FROM TestItems WHERE ItemID = 3 AND ItemResponse = 'FALSE'
WHERE (SELECT COUNT(*) FROM (
SELECT 1 FROM TestItems WHERE ItemID = 1 AND ItemResponse = 'TRUE'
UNION
SELECT * FROM TestItems WHERE ItemID = 3 AND ItemResponse = 'FALSE'
) AS t) >= 2
Your insert did not work because ItemID cant be both 1 and 3 at the same time. My solution gets the required records to be inserted into Scale table, but verifies both the record exists by checking the count. Additionally you could (should) do as below since this can be marginally more efficient (the above SQL was to clearly show the logic being used):
INSERT INTO Scale
SELECT * FROM TestItems WHERE ItemID = 1 AND ItemResponse = 'TRUE'
UNION
SELECT * FROM TestItems WHERE ItemID = 3 AND ItemResponse = 'FALSE'
WHERE (
SELECT COUNT(*)
FROM TestItems
WHERE ItemID = 1 AND ItemResponse = 'TRUE'
OR ItemID = 3 AND ItemResponse = 'FALSE'
) >= 2

Resources