Link to home
Start Free TrialLog in
Avatar of saturation
saturation

asked on

SqlDataSource: The server tag is not well formed.

I have a SQL data source and I'm trying to concatenate the logged in user with my SQL SELECT string.  I think I have the syntax wrong, though.   Can someone help?

 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
 ConnectionString="<%$ ConnectionStrings:SQLConnection %>"
 SelectCommand="SELECT dcca_firstname, FROM tblDCCreditApplication WHERE dcca_userid='" & DirectCast(Page, PageBaseClass).CurrentLoggedUser.ID & "'">
>

Avatar of Nathan Bove
Nathan Bove
Flag of United States of America image

The offending line is:
SelectCommand="SELECT dcca_firstname, FROM tblDCCreditApplication WHERE dcca_userid='" & DirectCast(Page, PageBaseClass).CurrentLoggedUser.ID & "'"

You are not allowed to set those values explicity inline.  
1)  Remove the "Select Command" property from your datasource.
2)  Add a "OnSelecting" event handler.
3)  In the event handler, add the following code:
SqlDataSource1.SelectCommand = "SELECT dcca_firstname, FROM tblDCCreditApplication WHERE dcca_userid='" & DirectCast(Page, PageBaseClass).CurrentLoggedUser.ID & "'"
Avatar of saturation
saturation

ASKER

How do I add the OnSelecting event handler?   Sorry, I'm fairly new to .NET.
Modify your datasource to read:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
 ConnectionString="<%$ ConnectionStrings:SQLConnection %>"
 onselecting="SqlDataSource1_Selecting">

Add the following method to the code behind:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) {
  SqlDataSource1.SelectCommand = "SELECT dcca_firstname, FROM tblDCCreditApplication WHERE dcca_userid='" & DirectCast(Page, PageBaseClass).CurrentLoggedUser.ID & "'"
}
Is this C#?   My page is VB.NET.
ASKER CERTIFIED SOLUTION
Avatar of Nathan Bove
Nathan Bove
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I get "Statement cannot appear within a method body. End of method assumed." with the code below.   What am I doing wrong?
<%

Public Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As SqlDataSourceSelectingEventArgs)
    SqlDataSource1.SelectCommand = "SELECT dcca_firstname FROM tblDCCreditApplication WHERE dcca_userid='" & DirectCast(Page, PageBaseClass).CurrentLoggedUser.ID & "'"

End Sub

%>

Open in new window