2

I'm getting this error, but I just can't tell where it might be coming from. The line that the error page is referencing is:

@ Page Title="" Language="C#" MasterPageFile="~/CV.Master" AutoEventWireup="true" CodeBehind="AddPost.aspx.cs" Inherits="CV_Blog_WDW.AddPost"

But I can't see how that line can cause that error? Unless there's something I'm missing?

My .aspx code is:

<%@ Page Title="" Language="C#" MasterPageFile="~/CV.Master" AutoEventWireup="true" CodeBehind="AddPost.aspx.cs" Inherits="CV_Blog_WDW.AddPost" %>
    <asp:Content ID="Content1"
                 ContentPlaceHolderID="ContentPlaceHolder1"
                 runat="server">
        <!-- =========
        Special Nav for BLog page
        ===================================-->
        <nav class="nav-blog">
            <a href="default.aspx"
               class="btn btn-left"
               data-toggle="tooltip"
               data-placement="left"
               title=""
               data-original-title="Home">
                <i class="fa fa-home"></i>
            </a>
            <a href="#"
               class="btn btn-big-blog">Blog</a>
            <a href="#"
               class="btn btn-right"
               data-toggle="tooltip"
               data-placement="right"
               title=""
               data-original-title="Reload Page">
                <i class="fa fa-refresh"></i>
            </a>
        </nav>
        <!-- =========
        Start Show Yor Name Section
        ===================================-->
        </div>
        </header>
        <!-- =========
    End portrait section
    ===================================-->
        <!-- =========
    Start Content section
    ===================================-->
        <section class="content open"
                 id="main-content">
            <div class="body-content"
                 id="blog">
                <div class="row">
                    <div class="col-md-10 col-md-offset-1">
                        <div class="blog-posts">
                            <div class="blog-post">
                                <h3 class="title with-icon">
                                    <span class="fa  fa-comment-o icn-title"></span> Add A Post
                                </h3>
                                <div class="box-block">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="Title">Title</label>
                                                <asp:TextBox ID="Title"
                                                             runat="server"
                                                             CssClass="form-control"></asp:TextBox>
                                            </div>
                                            <div class="form-group">
                                                <label for="FeaturedImage">Featured Image</label>
                                                <asp:FileUpload ID="FeaturedImage"
                                                                runat="server"
                                                                CssClass="form-control" />
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="MesageForm">Body</label>
                                        <asp:TextBox ID="Body"
                                                     TextMode="MultiLine"
                                                     Rows="8"
                                                     runat="server"
                                                     CssClass="form-control"></asp:TextBox>
                                    </div>
                                    <asp:Button id="btnAdd"
                                                runat="server"
                                                CssClass="btn btn-flat btn-lg"
                                                Text="Add Post"
                                                OnClick="btnAdd_Click" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    </asp:Content>

And my code behind is:

public partial class AddPost : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if(FeaturedImage.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FeaturedImage.FileName);
                FeaturedImage.SaveAs(Server.MapPath("~/assets/images/blog/") + filename);
            }
            catch(Exception ex)
            {
                string error = ex.Message;
            }
        }

        try
        {
            string connection = WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

            SqlConnection con = new SqlConnection(connection);

            SqlCommand cmd = new SqlCommand("AddPost", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Title", Title.Text);
            cmd.Parameters.AddWithValue("@Date", DateTime.Now);
            cmd.Parameters.AddWithValue("@FeatureImage", Path.GetFileName(FeaturedImage.FileName));
            cmd.Parameters.AddWithValue("@PostedBy", 1);
            cmd.Parameters.AddWithValue("@Body", Body.Text);

            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch(Exception ex)
        {

        }
    }
}

I've google'ed the error but there's nothing that seems relevant to my code? Really stumped on this one, not been able to try any fixes as to be honest not sure where to start. As far as I can see I'm not making a conversion, but maybe there's one going on that I'm, not aware of?

3
  • 2
    The eror comes from assigning a string to a TextBox variable. I don't see any such assignment in the code, so it might be from using textbox names that already exist. Try to rename the Title and Body textboxes. Commented Sep 21, 2015 at 23:46
  • @Guffa it would be good to move your answer so we can see it's been answered and voted on from the asp.net list of questions. Commented Sep 22, 2015 at 8:51
  • @LianeStevenson: Glad that it helped. :) I wrote an answer for it, fleshing it out a bit with the general cause of the exception and the specific cause. Commented Sep 22, 2015 at 12:09

1 Answer 1

6

The error comes from assigning a string to a TextBox variable. The usual reason is that one forgets the Text property, and use something like:

MyTextbox = "Some string";

intead of:

MyTextbox.Text = "Some string";

However, as there is no such code in your methods, and because the error message points to the aspx page, the error is somewhere in the code that is generated from the markup.

You have a text box named Title. There is already a string property by that name in the Page class, and when the generated code tries to set the string property, the assigment will use the TextBox field instead as it shadows the string property.

Rename the Title text box to something that isn't used already.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.