Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts

    setting literal text to win username on formview databind

    I am very new to c# and .net in general.

    Im getting a nullreference error, shocker.

    I have a simple form to add a new article to a database. the end goal is to be able to upload an image to the webserver and save content and the image name in a mssql database. I am using a formview control defaulted to the insert view.

    I added a Literal to the insert view and I am trying to change the text of that literal to the username of the current user ( its going on an intranet site so windows auth). When I try to set the text of the literal i get the nullreference.


    Code:
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace testcms2.eat
    {
        public partial class add : System.Web.UI.Page
        {
            public string now;
            protected void Page_Load(object sender, EventArgs e)
            {
    
                
            }
            protected void FormViewCommandEventHandler(Object sender, FormViewCommandEventArgs e)
            {
                //doesnt work yet
                if (e.CommandName == "Add")
                {
                    errors.Text = "You tried to submit something";
                    errorcontainer.Visible = true;
                }
    
            }
    
            protected void FormView1_DataBound(object sender, EventArgs e)
            {
                DateTime now = DateTime.Now;
                String strName;
                strName = HttpContext.Current.User.Identity.Name.ToString();
                TextBox postedby = FormView1.FindControl("PostedBy") as TextBox;
                TextBox posteddate = FormView1.FindControl("PostedDate") as TextBox;
               postedby.Text = strName.ToString();//get the error here on the postedby object I assume
                posteddate.Text = now.ToString();
            }
    
        }
    }

    Code:
    <%@ Page Title="" Language="C#" MasterPageFile="~/CMS.Master" AutoEventWireup="true" CodeBehind="add.aspx.cs" Inherits="testcms2.eat.add" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:Panel ID="errorcontainer" CssClass="errorcontainer" runat="server" EnableViewState="false">
            <asp:Literal ID="errors" runat="server">
            </asp:Literal>
        </asp:Panel>
        <asp:FormView ID="FormView1" runat="server" CellPadding="4" 
            DataKeyNames="ID_EAT" DataSourceID="SqlDataSource1" ForeColor="#333333" 
            DefaultMode="Insert" OnItemCommand="FormViewCommandEventHandler" 
            ondatabound="FormView1_DataBound">
            <EditItemTemplate>
                ID_EAT:
                <asp:Label ID="ID_EATLabel1" runat="server" Text='<%# Eval("ID_EAT") %>' />
                <br />
                Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                Article:
                <asp:TextBox ID="ArticleTextBox" runat="server" Text='<%# Bind("Article") %>' />
                <br />
                URL:
                <asp:TextBox ID="URLTextBox" runat="server" Text='<%# Bind("URL") %>' />
                <br />
                Image1:
                <asp:TextBox ID="Image1TextBox" runat="server" Text='<%# Bind("Image1") %>' />
                <br />
                ModifiedDate:
                <asp:TextBox ID="ModifiedDateTextBox" runat="server" 
                    Text='<%# Bind("ModifiedDate") %>' />
                <br />
                PostDate:
                <asp:TextBox ID="PostDateTextBox" runat="server" 
                    Text='<%# Bind("PostDate") %>' />
                <br />
                PostedBy:
                <asp:TextBox ID="PostedByTextBox" runat="server" 
                    Text='<%# Bind("PostedBy") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <InsertItemTemplate>
               Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                Article:
                <asp:TextBox ID="ArticleTextBox" runat="server" Text='<%# Bind("Article") %>' />
                <br />
                URL:
                <asp:TextBox ID="URLTextBox" runat="server" Text='<%# Bind("URL") %>' />
                <br />
                Image1:
                <asp:TextBox ID="Image1TextBox" runat="server" Text='<%# Bind("Image1") %>' />
                <br />
                PostDate:
                <asp:Literal ID="PostDate" runat="server"></asp:Literal>
                <br />
                PostedBy:
                <asp:Literal ID="PostedByUsername" runat="server"></asp:Literal>
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="Insert" />
            </InsertItemTemplate>
            <ItemTemplate>
                ID_EAT:
                <asp:Label ID="ID_EATLabel" runat="server" Text='<%# Eval("ID_EAT") %>' />
                <br />
                Title:
                <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                Article:
                <asp:Label ID="ArticleLabel" runat="server" Text='<%# Bind("Article") %>' />
                <br />
                URL:
                <asp:Label ID="URLLabel" runat="server" Text='<%# Bind("URL") %>' />
                <br />
                Image1:
                <asp:Label ID="Image1Label" runat="server" Text='<%# Bind("Image1") %>' />
                <br />
                ModifiedDate:
                <asp:Label ID="ModifiedDateLabel" runat="server" 
                    Text='<%# Bind("ModifiedDate") %>' />
                <br />
                PostDate:
                <asp:Label ID="PostDateLabel" runat="server" Text='<%# Bind("PostDate") %>' />
                <br />
                PostedBy:
                <asp:Label ID="PostedByLabel" runat="server" Text='<%# Bind("PostedBy") %>' />
                <br />
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                    CommandName="Edit" Text="Edit" />
                &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                    CommandName="Delete" Text="Delete" />
                &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                    CommandName="New" Text="New" />
            </ItemTemplate>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        </asp:FormView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:WebOperationsConnectionString %>" 
            DeleteCommand="DELETE FROM [12_EAT] WHERE [ID_EAT] = @ID_EAT" 
            InsertCommand="INSERT INTO [12_EAT] ([Title], [Article], [URL], [Image1], [ModifiedDate], [PostDate], [PostedBy]) VALUES (@Title, @Article, @URL, @Image1, @ModifiedDate, @PostDate, @PostedBy)" 
            SelectCommand="SELECT [ID_EAT], [Title], [Article], [URL], [Image1], [ModifiedDate], [PostDate], [PostedBy] FROM [12_EAT]" 
            UpdateCommand="UPDATE [12_EAT] SET [Title] = @Title, [Article] = @Article, [URL] = @URL, [Image1] = @Image1, [ModifiedDate] = @ModifiedDate, [PostDate] = @PostDate, [PostedBy] = @PostedBy WHERE [ID_EAT] = @ID_EAT">
            <DeleteParameters>
                <asp:Parameter Name="ID_EAT" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Article" Type="String" />
                <asp:Parameter Name="URL" Type="String" />
                <asp:Parameter Name="Image1" Type="String" />
                <asp:Parameter Name="ModifiedDate" Type="DateTime" />
                <asp:Parameter Name="PostDate" Type="DateTime" />
                <asp:Parameter Name="PostedBy" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Article" Type="String" />
                <asp:Parameter Name="URL" Type="String" />
                <asp:Parameter Name="Image1" Type="String" />
                <asp:Parameter Name="ModifiedDate" Type="DateTime" />
                <asp:Parameter Name="PostDate" Type="DateTime" />
                <asp:Parameter Name="PostedBy" Type="String" />
                <asp:Parameter Name="ID_EAT" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </asp:Content>
    Last edited by DanInMa; 01-10-2013 at 05:54 AM.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    nvmind i fixed it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •