Showing posts with label single. Show all posts
Showing posts with label single. Show all posts

Thursday, March 29, 2012

Getting a single value from SqlDataSource

Hi,

I have a SqlDataSource(named SQLDS1) which retrieves 4 value from database(ProductName,ProductCost,ProductID). I Have a DropDownList(DDL1) control and its DataSource SQLDS1.

DDL1 Selected data field todisplay is ProductName
DDL1 Selected data field tovalue is ProductCost

I did all this in Visual Part without any line of code. But in the code behind , When i select an item from DDL1 i need its ProductName,ProductCost and Also ProductID. It is simple to get first two. But how can i get the ProductID. Is there anyway to get ProductID from SQLDS1.

Happy Coding

Hi

You will be able to obtain only the values that you bind to the drop down list. if you want non bound value then you will have to fire another query based on what you select from the drop down.

For example,

select productid from Table_name where product name = 'prodname you selected from ddl' and productcost= cost you got from ddl.

Then use productid as you need

HTH

Friday, February 24, 2012

Get Period of Dates

Hi,
Just wonder if i can get a period of dates to be inserted into a temp table (with a single field [Sales_Date]) base on a Start and End Date using a select query?
For Eg,
Start = '8/1/2005', End = '8/5/2005'
In the temp table,
8/1/2005
8/2/2005
8/3/2005
8/4/2005
8/5/2005
Your help is appreciated. Thks.
Rgds
Ryanyou could do something like :

declare @.sdate datetime, @.edate datetime
select
@.sdate = '08/11/2005'
,@.edate = '08/15/2005'
create table #t (datecol datetime)
insert into #t values (@.sdate)
while datediff(d,@.sdate, @.edate) > 0
begin
insert into #t values (dateadd(d, 1,@.sdate ))
set @.sdate = dateadd(d,1, @.sdate)
end
select * from #t
drop table #t|||

Hi,

Your method works. Anyway, aApart from using a temp table, any other more efficient way to get the same outcome? Let me know. Thks.

Ryan

|||The only more efficient way I can think of is to physcially create atable that contains all of the dates and leave that sitting on disk.

Sunday, February 19, 2012

Get only single row results per id?

Hi, is it possible to make an sql query that has an Outer Join but return only one row of results max per id.

For example i have an Articles table, and a PicturesForArticles table.

The Articles table has an id field(aid), a title field(aTitle) and a content field(aContent).

And the PicturesForArticles table has an id field(pid), a PicPath filed and a field linking it to the articles table(aid)

Obviously the PicturesForArticles field stores pictures for the articles, and article can have a multiple number of pictures, or no pictures at all.

So i want to make a query that will return all of the Articles fields and a picture for each article. Even if the article has many pictures i only want to get a single row for each aid(Articles Id), and if there are no pictures for that article the picture fields will be null.

Is there any way to do this, to only return on row of results for each aid?

Thanks

That's very similar to something that I discussed in one of my articles on SingingEels :http://www.singingeels.com/Articles/How_To_Maintain_Customer_Payment_History.aspx

Basically, you can use the "SubSelect" method I did under the "Joining The Tables Together" subheading.

Let me know if you need more help with this, if not, then please mark this post as the answer.

Thanks,

|||

Did you mean the first query after the subheading?

If so then how can i select more than one field from the derived table, do i need another derived table?

|||

At the end of the article I show how to get more than one field from a 'vertical' table, but grouped by a certain criteria (in my case "CustomerID")

SELECT
Customers.*,
LatestPayments.*
FROM
dbo.CustomersLEFTOUTERJOIN
(SELECT CustomerID,MAX(ID)AS LastPaymentID,MAX(PaymentDate)AS LastPaymentDate
FROM dbo.PaymentHistoryGROUPBY CustomerID) LatestPayments
ON Customers.ID = LatestPayments.CustomerID

That's the code fromhttp://www.singingeels.com/Articles/How_To_Maintain_Customer_Payment_History.aspx that shows how to do that... if you are having troubles modifying it to your needs, then please past your table definitions and I'll change it to work for you.

Thanks,

|||

Ok thanks