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 9th, 2006, 03:33 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you again! I pasted the code into the page. I was using a sqldatasource, so I changed it to an ObjectDataSource to match your code.

1. I'm not sure if the parameter needs to be in the DataSet if it is in the select query.
2. I'm getting an error "End of Statement" expected.

Here is the new code.

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="accountID"
        DataSourceID="ObjectDataSource1" Height="50px" Width="125px">
        <Fields>
            <asp:BoundField DataField="accountID" HeaderText="accountID" InsertVisible="False"
                ReadOnly="True" SortExpression="accountID" />
            <asp:BoundField DataField="accountUserName" HeaderText="accountUserName" SortExpression="accountUserName" />
            <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
            <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
        </Fields>
    </asp:DetailsView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
        TypeName="dsAccountDetailsTableAdapters.tblUserDet ailsTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_accountID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="Original_accountID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>



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

Sorry about that. This should work equally well with a SqlDataSource:
Code:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
        Handles SqlDataSource1.Selecting
  e.Command.Parameters("UserName").Value = User.Identity.Name
End Sub
Where do you get the error you mentioned? At run time or at compile time?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old June 9th, 2006, 05:34 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get the error at compile...

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

Also with the Selecting event of a SqlDataSource?

Something else must be going wrong. Doesn't Visual Studio tell you where the error occurred, making it easier to see what went wrong?

Can you post your code?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old June 12th, 2006, 04:16 PM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried the following code from this thread.

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventAr gs) _
   Handles DetailsView1.ItemInserting

  e.Values("UserName") = User.Identity.Name

End Sub

I am using C#, but I encountered an error telling me that e.Values is a property and I am using it as a method. Did I miss something on the translation?

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values("postedBy") = Membership.GetUser().UserName;
    }

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

e.Values("postedBy") is VB syntax for a collection indexer. In C#, the compiler thinks you're trying to call a method named Values and pass it the literal string "postedBy:.

Use [] instead:

e.Values["postedBy"]

Cheers,

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

Hi Imar!

I had to take a brain break from this. The error comes up...

"End of Statement expected" Line 7, Column 51

The following underlined text is underlined in a blue squiggle:

    Protected Sub ObjectDataSource1_Selecting(ByVal sender As Object, _
              ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectin gEventArgs) _
              Handles ObjectDataSource1.Selecting e.InputParameters("UserName") = User.Identity.Name
    End Sub


Here is the page code:

    <form id="form1" runat="server">
    <div>
        &nbsp;Welcome
        <asp:LoginName ID="lblLogInName" runat="server" />
        !<br />
        <br />
    </div>
        &nbsp; &nbsp; &nbsp;
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
            DefaultMode="Insert" Height="50px" Width="125px" DataKeyNames="loginName" DataMember="DefaultView" EmptyDataText="No Data">
            <Fields>
                <asp:TemplateField HeaderText="accountUserName" SortExpression="accountUserName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:Label ID="lblAcctUserName" runat="server" OnDataBinding="Page_Load" Text='<%# Eval("accountUserName") %>'></asp:Label>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
                <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
                <asp:TemplateField ShowHeader="False">
                    <InsertItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert"
                            OnClick="Button1_Click" Text="Insert" />&nbsp;<asp:Button ID="Button2" runat="server"
                                CausesValidation="False" CommandName="Cancel" Text="Cancel" Visible="False" />
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="New"
                            Text="New" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connCPFAAEvents %>"
            DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO tblUserDetails(accountUserName, accountFirstName, accountLastName) VALUES (@UserName, @accountFirstName, @accountLastName)"
            ProviderName="<%$ ConnectionStrings:cpfaa.orgConnectionString1.Provi derName %>"
            SelectCommand="SELECT accountUserName, accountFirstName, accountLastName FROM tblUserDetails"
            UpdateCommand="UPDATE [tblUserDetails] SET [accountUserName] = 'TEST', [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
            <DeleteParameters>
                <asp:Parameter Name="accountID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="UserName" />
                <asp:Parameter Name="accountFirstName" Type="String" />
                <asp:Parameter Name="accountLastName" Type="String" />
                <asp:Parameter Name="accountID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="UserName" DefaultValue = "Test"/>
                <asp:Parameter Name="accountFirstName" Type="String" />
                <asp:Parameter Name="accountLastName" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        &nbsp;
    </form>

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

You're missing a crucial break, between the method declaration and its implementation:

Protected Sub ObjectDataSource1_Selecting(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectin gEventArgs) _
              Handles ObjectDataSource1.Selecting

  e.InputParameters("UserName") = User.Identity.Name

End Sub

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old June 14th, 2006, 04:11 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The Process -> They create account, land on page completeAccount.aspx where the username field is populated. They fill in their details (First Name and Last Name for now). On submit, they are redirected to page userDetails.aspx where their details are confirmed.

Okay...so code for page completeAccount.aspx:

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" Height="50px"
        Width="125px" AutoGenerateRows="False" DefaultMode="Insert">
        <Fields>
            <asp:BoundField DataField="accountID" HeaderText="accountID" InsertVisible="False"
                ReadOnly="True" SortExpression="accountID" />
            <asp:BoundField DataField="userName" HeaderText="userName" SortExpression="userName" />
            <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
            <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
            <asp:TemplateField ShowHeader="False">
                <InsertItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert"
                        OnClick="Button1_Click" Text="Insert" />&nbsp;<asp:Button ID="Button2" runat="server"
                            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="New"
                        Text="New" />
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>"
        DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO [tblUserDetails] ([userName], [accountFirstName], [accountLastName]) VALUES (@userName, @accountFirstName, @accountLastName)"
        SelectCommand="SELECT [accountID], [userName], [accountFirstName], [accountLastName] FROM [tblUserDetails]"
        UpdateCommand="UPDATE [tblUserDetails] SET [userName] = @userName, [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
        <DeleteParameters>
            <asp:Parameter Name="accountID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="accountID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

CODE BEHIND for completeAccount.aspx

Partial Class adminTest_completeAccount
    Inherits System.Web.UI.Page

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, _
       ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventAr gs) _
       Handles DetailsView1.ItemInserting
        e.Values("userName") = User.Identity.Name

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("userDetails.aspx")
    End Sub
End Class

CODE FOR userDetails.aspx

Welcome Back
    <asp:LoginName ID="LoginName1" runat="server" />
    !<br />
    <br />
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataSourceID="SqlDataSource1" Height="50px" Width="125px">
    </asp:DetailsView>
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
        ConnectionString="<%$ ConnectionStrings:conn %>" DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @original_accountID AND [userName] = @original_userName AND [accountFirstName] = @original_accountFirstName AND [accountLastName] = @original_accountLastName"
        InsertCommand="INSERT INTO [tblUserDetails] ([userName], [accountFirstName], [accountLastName]) VALUES (@userName, @accountFirstName, @accountLastName)"
        OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [accountID], [userName], [accountFirstName], [accountLastName] FROM [tblUserDetails]"
        UpdateCommand="UPDATE [tblUserDetails] SET [userName] = @userName, [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @original_accountID AND [userName] = @original_userName AND [accountFirstName] = @original_accountFirstName AND [accountLastName] = @original_accountLastName">
        <DeleteParameters>
            <asp:Parameter Name="original_accountID" Type="Int32" />
            <asp:Parameter Name="original_userName" Type="String" />
            <asp:Parameter Name="original_accountFirstName" Type="String" />
            <asp:Parameter Name="original_accountLastName" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="original_accountID" Type="Int32" />
            <asp:Parameter Name="original_userName" Type="String" />
            <asp:Parameter Name="original_accountFirstName" Type="String" />
            <asp:Parameter Name="original_accountLastName" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

CODE BEHIND FOR userDetails.aspx


Partial Class adminTest_userDetails
    Inherits System.Web.UI.Page


    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

End Class


Thanks Again!

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

You're welcome. Does it work now?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





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.