Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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 July 9th, 2012, 08:21 AM
Registered User
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default Setting default values in a listview insert

I have a comments section on my website which allows logged in users to add short comments.

Comments should write to a table called "News", which is used to store a variety of content for the site.

I use a textbox to allow the user to enter into the column "Summary", and I want the Date, NewsCategory and SubmittedBy columns to have automatic values.

I've tried to do this in the code behind

Code:
    protected void CommentSqlDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        CommentSqlDataSource.InsertParameters["Date"].DefaultValue = DateTime.Now.ToString();
        CommentSqlDataSource.InsertParameters["NewsCategory"].DefaultValue = "8"; // 8 is Id for a comment
        CommentSqlDataSource.InsertParameters["SubmittedBy"].DefaultValue = User.Identity.Name.ToString();
    }
I get and error though, saying "Cannot insert the value NULL into column 'Date', table 'C:\BEGASPNET\MIDDLERIPSLEY\APP_DATA\ASPNETDB.MDF. dbo.News'; column does not allow nulls. INSERT fails.
The statement has been terminated."


My page has the code...

Code:
    <asp:SqlDataSource ID="CommentSqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [News] WHERE [NewsId] = @original_NewsId" 
        InsertCommand="INSERT INTO [News] ([Date], [NewsCategory], [Headline], [Summary], [Main], [SubmittedBy], [Approved], [PictureUrl]) VALUES (@Date, @NewsCategory, @Headline, @Summary, @Main, @SubmittedBy, @Approved, @PictureUrl)" 
        OldValuesParameterFormatString="original_{0}" 
        SelectCommand="SELECT * FROM [News]" 
        UpdateCommand="UPDATE [News] SET [Date] = @Date, [NewsCategory] = @NewsCategory, [Headline] = @Headline, [Summary] = @Summary, [Main] = @Main, [SubmittedBy] = @SubmittedBy, [Approved] = @Approved, [PictureUrl] = @PictureUrl WHERE [NewsId] = @original_NewsId" 
        oninserting="CommentSqlDataSource_Inserting">

        <DeleteParameters>
            <asp:Parameter Name="original_NewsId" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter DbType="Date" Name="Date" />
            <asp:Parameter Name="NewsCategory" Type="Int32" />
            <asp:Parameter Name="Headline" Type="String" />
            <asp:Parameter Name="Summary" Type="String" />
            <asp:Parameter Name="Main" Type="String" />
            <asp:Parameter Name="SubmittedBy" Type="String" />
            <asp:Parameter Name="Approved" Type="Boolean" />
            <asp:Parameter Name="PictureUrl" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter DbType="Date" Name="Date" />
            <asp:Parameter Name="NewsCategory" Type="Int32" />
            <asp:Parameter Name="Headline" Type="String" />
            <asp:Parameter Name="Summary" Type="String" />
            <asp:Parameter Name="Main" Type="String" />
            <asp:Parameter Name="SubmittedBy" Type="String" />
            <asp:Parameter Name="Approved" Type="Boolean" />
            <asp:Parameter Name="PictureUrl" Type="String" />
            <asp:Parameter Name="original_NewsId" Type="Int32" />
        </UpdateParameters>

    </asp:SqlDataSource>
    <asp:LoginView ID="CommentLoginView" runat="server" EnableViewState="false"  >
        <AnonymousTemplate>
            <asp:Label ID="CommentsLogInLabel" runat="server" CssClass="menuboxes" Text="You need to be logged in to add comments" />
            <br />
            <br />
            Clich here to <a href="~/Account/Login.aspx" ID="CommentLoginStatus" runat="server" class="menuboxes">Log In</a> 
        </AnonymousTemplate>
        <LoggedInTemplate>
           <asp:Label ID="CommentLabel" runat="server" Text="Use the form below to add a comment.<br /><br />Your comments will appear on the Home Page." CssClass="NewsSummary" />
            <br />
            <asp:ListView ID="LogginInListView" runat="server" DataSourceID="CommentSqlDataSource" 
                DataKeyNames="NewsId" InsertItemPosition="LastItem">

                <EmptyDataTemplate>
                    <span>No comments.</span>
                </EmptyDataTemplate>
                <InsertItemTemplate>
                  <span style="">
                    <br />
                    Your Comment:
                    <br />
                    <asp:TextBox ID="SummaryTextBox" runat="server" Text='<%# Bind("Summary") %>'  TextMode="MultiLine" Width="300px" />
                    <br />
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert"  Text="Insert" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"  Text="Clear" />
                    <br />
                    <br />
                  </span>
                </InsertItemTemplate>
                <ItemTemplate>
                    <span style=""></span>
                </ItemTemplate>
                <LayoutTemplate>
                    <div ID="itemPlaceholderContainer" runat="server" style="">
                        <span runat="server" id="itemPlaceholder" />
                    </div>
                    <div style="">
                    </div>
                </LayoutTemplate>

            </asp:ListView>

        </LoggedInTemplate>
     </asp:LoginView>
Any ideas?
 
Old July 12th, 2012, 01:25 PM
Authorized User
 
Join Date: Jul 2012
Posts: 21
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Your issue is most likely related to the fact that you have the InsertParameter on the data source specified as a DbType of date, but your specifiying the insert parameter in code with a ToString() value. Why the mismatch?

Frankly, I take the easiest route in cases like this. I would just skip the insert statement of that date altogether, and let the database do the work by setting the default value on the database field with the (GETDATE()) method.
The Following User Says Thank You to guenfire For This Useful Post:
RichardL1969 (July 14th, 2012)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting a DetailsView Insert default value jpullam ASP.NET 2.0 Basics 13 April 7th, 2014 02:19 AM
Default values when setting up an object data sour mikener ASP.NET 2.0 Professional 0 November 3rd, 2008 09:12 AM
Setting Default values of DataGrid in C#2005 saxitalis C# 2005 0 August 15th, 2007 03:33 PM
setting default values prithvi28 XSLT 1 October 3rd, 2006 01:38 PM
setting default month jtyson ASP.NET 1.0 and 1.1 Basics 1 June 30th, 2003 12:44 PM





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