Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

in my code behind i have a function getEmail() that returns an email address, in the front end i have a sql data source selectcommand that grabs info according to the email address. as of now the email address is hardcoded i was wondering how to put getEmail() in to the selectcommand.

VB

    Public Function getEmail() As String
     Dim x As PublicProfile
     x= UserProfileContext.Current.UserInfo
    Return x.LoginName.ToString()
   End Function

front end

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="select Count(ID)nums from revs where Email='[email protected]' and Source='PUB' and Status='a'"></asp:SqlDataSource>

I tried putting <% =getEmail() %> instead of the email but had no luck

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="select Count(ID)nums from revs where Email='<% =getEmail() %>' and Source='PUB' and Status='a'"></asp:SqlDataSource>
share|improve this question
    
Easy but prolix so I link MSDN article: msdn.microsoft.com/en-us/library/…. In short use SQL parameters and fill them when page loads. – Adriano Repetti Jun 11 at 15:07
    
im reading that now but im not sure how to use my function to change the select parameter – bigdowg Jun 11 at 15:12
    
When you say you "had no luck," what happened? Blanks, errors, bats flying out of the screen :) ? – David W Jun 11 at 15:13
    
lol it just shows nothing as if i entered nothing as an email – bigdowg Jun 11 at 15:14

1 Answer 1

up vote 1 down vote accepted

To parameterize your query, modify your SqlDataSource markup as follows to add an "@Email" SelectParameter:

 <asp:SqlDataSource> 
      ID="SqlDataSource1" 
   runat="server" 
   ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
   SelectCommand="select Count(ID)nums from revs where Email=@EMail and  
                  Source='PUB' and Status='a'">
</asp:SqlDataSource>

In your codebehind/Page_Load(), try adding the following (before your binding) to place a value into the SelectParameter defined in the markup, which is (in turn) provided by your function:

SqlDataSource1.SelectParameters("Email").DefaultValue = getEmail()
share|improve this answer
1  
beautiful my friend, stack needs more like u – bigdowg Jun 11 at 15:42
    
LOL thanks :) Glad to help. – David W Jun 11 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.