Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2013, 05:29 AM   PM User | #1
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
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>
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 01-10-2013 at 05:54 AM..
DanInMa is offline   Reply With Quote
Old 01-10-2013, 05:54 AM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
nvmind i fixed it.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 AM.


Advertisement
Log in to turn off these ads.