Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 14th, 2006, 04:42 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, Imar, you've been so helpful!

No, it isn't working. The code I posted does not include the parameter from the page completeAccount.aspx to userDetails.aspx. I added the parameter to the redirect code (response.redirect(userDetails.aspx?username={0}). When I did this, I get an exception.

I am using the SQLDataSource code....

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEv entArgs) _
            Handles SqlDataSource1.Selecting
        e.Command.Parameters("UserName").Value = User.Identity.Name
    End Sub

This is the exception error:

System.IndexOutOfRangeException was unhandled by user code
  Message="An SqlParameter with ParameterName 'UserName' is not contained by this SqlParameterCollection."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlParameterCollection.GetPa rameter(String parameterName)
       at System.Data.Common.DbParameterCollection.get_Item( String parameterName)
       at adminTest_userDetails.SqlDataSource1_Selecting(Obj ect sender, SqlDataSourceSelectingEventArgs e) in C:\Documents and Settings\shari.PEAKBIZ\My Documents\Visual Studio 2005\WebSites\cpfaa\adminTest\userDetails.aspx.vb: line 8
       at System.Web.UI.WebControls.SqlDataSourceView.OnSele cting(SqlDataSourceSelectingEventArgs e)
       at System.Web.UI.WebControls.SqlDataSourceView.Execut eSelect(DataSourceSelectArguments arguments)
       at System.Web.UI.DataSourceView.Select(DataSourceSele ctArguments arguments, DataSourceViewSelectCallback callback)
       at System.Web.UI.WebControls.DataBoundControl.Perform Select()
       at System.Web.UI.WebControls.BaseDataBoundControl.Dat aBind()
       at System.Web.UI.WebControls.DetailsView.DataBind()
       at System.Web.UI.WebControls.BaseDataBoundControl.Ens ureDataBound()
       at System.Web.UI.WebControls.DetailsView.EnsureDataBo und()
       at System.Web.UI.WebControls.CompositeDataBoundContro l.CreateChildControls()
       at System.Web.UI.Control.EnsureChildControls()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


 
Old June 14th, 2006, 06:06 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe the problem is in the select query...I've tried it as a session, as a querystring...several of the options and none have worked. What should this be if it is based on the selecting event code you provided?

        <SelectParameters>
            <asp:SessionParameter Name="userName" SessionField="username" Type="String" />
        </SelectParameters>

 
Old June 17th, 2006, 02:28 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar -

Have you had a chance to look over the new post, or does anyone else have an idea? Thank you!

 
Old June 18th, 2006, 07:17 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Are you storing the user name in a Session field called UserName? If not, this code will never work.

Did you try a normal parameter, like this:

<asp:Parameter Name="UserName" />

in combination with the selecting event?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Mantra by Tool (Track 4 from the album: Lateralus) What's This?
 
Old December 7th, 2006, 01:54 PM
saf saf is offline
Authorized User
 
Join Date: Nov 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar

I need to do something similar and I know the answer is in this post somewhere I just can't seem to put it in the right place..

All I want to do is use the currently logged in user's name (user.identity.name) in an sqldatasource control as a select parameter (e.g where studentname = user.identity.name) I can't figure out how to code the query to use this???? I searched and read posts on using expression builders and created custom parameters but there must be an easier way. only authenticated users get to my page, user.identity.name returns the name I want to query my database by....please help.

saf

 
Old December 7th, 2006, 05:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi saf,

Did you try my suggestion from my last post in this thread? E.g. set up a param like this:

<asp:Parameter Name="UserName" />

and then use the Selecting event of the data source to fill that parameter with the user's name?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old December 7th, 2006, 08:59 PM
saf saf is offline
Authorized User
 
Join Date: Nov 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Afraid I'm having the same problem as shario. I get error message:

An SqlParameter with ParameterName 'SchoolName' is not contained by this SqlParameterCollection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: An SqlParameter with ParameterName 'SchoolName' is not contained by this SqlParameterCollection.

Source Error:


Line 20:
Line 21: Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEv entArgs) Handles SqlDataSource1.Selecting
Line 22: e.Command.Parameters("SchoolName").Value = User.Identity.Name
Line 23:
Line 24: End Sub

What am I doing wrong???. Here is my sqldatasource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:LunchOrderConnectionString %>" SelectCommand="SELECT [CategoryName], [CategoryItemName], [Price] FROM [vw_OrderForm] WHERE ([SchoolName] = @SchoolName)">
        <SelectParameters>
            <asp:Parameter Name="SchoolName" Type="String" />
        
        </SelectParameters>
    </asp:SqlDataSource>

As always, I appreciate your time and patience....
 
Old December 8th, 2006, 03:57 AM
saf saf is offline
Authorized User
 
Join Date: Nov 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nevermind....

I just used a hidden field valued with user.identity.name instead. Works just the same...Can't believe I didn't think of that 10 hours ago....

Thanks anyway

 
Old December 8th, 2006, 08:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I just played around with this a little, and it seems that for SQL parameters you need to prefix the name with an @ symbol, just as in the query. The following code worked for me:
Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="YourConnectionString"
  SelectCommand="SELECT [CreateAuthor] FROM [Content] WHERE ([CreateAuthor] = @CreateAuthor)">
  <SelectParameters>
    <asp:Parameter Name="CreateAuthor" Type="Object" />
  </SelectParameters>
</asp:SqlDataSource>
Then fill the parameter in the Selecting event like this:
Code:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
      Handles SqlDataSource1.Selecting

  e.Command.Parameters.Item("@CreateAuthor").Value = _
      Membership.GetUser().ProviderUserKey

End Sub
Instead of the Guid from Membership.GetUser().ProviderUserKey you can of course have a String parameter and fill it with User.Identity.Name.


Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing images in Database and displaying them Manoj Bisht ASP.NET 1.x and 2.0 Application Design 4 February 6th, 2010 03:06 AM
Storing Images into database usprasad_m SQL Server 2000 4 May 17th, 2007 05:16 PM
Storing Videos to Database... ankur_icfai SQL Server 2000 2 October 19th, 2006 01:18 AM
Chapter 13 - Storing Inf in a Database phudong3da Dreamweaver (all versions) 1 April 22nd, 2005 01:00 AM
storing an image in a database nagdandey Classic ASP Databases 2 December 17th, 2003 05:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.