Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# Headwrecker...

Options
  • 18-01-2009 4:21am
    #1
    Closed Accounts Posts: 7,097 ✭✭✭


    I've got a headwrecking C# problem along the lines of the thread I started below a day or two ago but I've tidied up the description of the problem a bit here...

    I've done a screen capture of the MS 2005 Visual Web Developer error & warning list and attached it to this post...

    The code I've attached does actually work and upload an image file to my database, which is all I've been trying to achieve...

    But I want to import this solution into my website and I don't want a load of weird looking errors in it when I do...

    The solution I've found on the net...

    http://www.aspfree.com/c/a/ASP.NET/Uploading-Images-to-a-Database--C---Part-I/

    ...seems to have been written for an earlier version of ASP.NET and uses the "Codebehind="default.aspx.cs" keyword in the Default.aspx file.

    When I try to compile this page, I'm told that "Codebehind" is no longer supported, so I tried to change it to CodeFile="Default.aspx.cs"...

    When I do this, I get an error message saying that I'm "Missing a partial modifier on declaration of type"...

    I've googled the error and it would seem that if I change:

    public class UploadImage : System.Web.UI.Page

    to public partial class UploadImage : System.Web.UI.Page

    That this will resolve the issue, but when I do this, I get the 8 errors you can see on my attached image, saying mainly that "The Type 'DBImages.UploadImage' already contains a definition for 'xxxxxxxxx'.

    Any ideas???

    I need CodeFile to work for this solution because this whole solution will be going into another aspx page with a .cs page...


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 8,956 Mod ✭✭✭✭mewso


    In that image you are still using codebehind and I see an src too. Just use CodeFile="Default.aspx.cs" and Inherits="UploadImage" i.e. remove the DBImages. bit. Thats going back to .net 1.1 when you would reference the namespace then the class name. I think in version 2 onwards the namespace is implied.


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    musician wrote: »
    In that image you are still using codebehind and I see an src too. Just use CodeFile="Default.aspx.cs" and Inherits="UploadImage" i.e. remove the DBImages. bit. Thats going back to .net 1.1 when you would reference the namespace then the class name. I think in version 2 onwards the namespace is implied.

    Hi Musician,

    Thanks for the reply. I've made the changes you suggest above but still have the same 8 errors (see attached pic).

    These 8 errors can be grouped into two groups/types (errors 1-4 are the same as are errors 5-8), so I'm hoping that two small changes to code would resolve this. I haven't done classes, namespaces, structs, methods, etc since second year in uni and this was when doing a module in C (and I don't think I ever really understood it, just got through it for the sake of passing!).

    I can't attach the aspx and cs pages here so I've pasted them below... My connection string is in a web.config file and is working grand...

    //C# page.....

    using System;
    using System.Configuration;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.IO;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace DBImages
    {
    public partial class UploadImage : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button UploadBtn;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.HtmlControls.HtmlInputText txtImgName;
    protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;

    public UploadImage() { }

    private void Page_Load(object sender, System.EventArgs e) { }

    public void UploadBtn_Click(object sender, System.EventArgs e)
    {
    if (Page.IsValid) //save the image
    {
    Stream imgStream = UploadFile.PostedFile.InputStream;
    int imgLen = UploadFile.PostedFile.ContentLength;
    string imgContentType = UploadFile.PostedFile.ContentType;
    string imgName = txtImgName.Value;
    byte[] imgBinaryData = new byte[imgLen];
    int n = imgStream.Read(imgBinaryData, 0, imgLen);

    int RowsAffected = SaveToDB(imgName, imgBinaryData, imgContentType);
    if (RowsAffected > 0)
    {
    Response.Write("<BR>The Image was saved");
    }
    else
    {
    Response.Write("<BR>An error occurred uploading the image");
    }

    }
    }


    private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype)
    {
    //use the web.config to store the connection string
    SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
    SqlCommand command = new SqlCommand("UPDATE Vehicle_Data SET img_name = @img_name, img_data = @img_data, img_contenttype = @img_contenttype WHERE Stock_Code = '" + txtImgName.Value + "'", connection);
    SqlParameter param0 = new SqlParameter("@img_name&quot;, SqlDbType.VarChar, 50);
    param0.Value = imgName;
    command.Parameters.Add(param0);

    SqlParameter param1 = new SqlParameter("@img_data&quot;, SqlDbType.Image);
    param1.Value = imgbin;
    command.Parameters.Add(param1);

    SqlParameter param2 = new SqlParameter("@img_contenttype&quot;, SqlDbType.VarChar, 50);
    param2.Value = imgcontenttype;
    command.Parameters.Add(param2);

    connection.Open();
    int numRowsAffected = command.ExecuteNonQuery();
    connection.Close();

    return numRowsAffected;
    }
    }
    }



    //aspx page...

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="UploadImage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

    <html xmlns="http://www.w3.org/1999/xhtml&quot; >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>

    <form enctype="multipart/form-data" runat="server" id="form2">
    <h3>The ASPFree Friendly Image Uploader</h3>
    Enter A Friendly Name<input type="text" id="txtImgName" runat="server" />
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Required" ControlToValidate="txtImgName"></asp:RequiredFieldValidator>

    <br />Select File To Upload:
    <input id="UploadFile" type="file" runat="server" />
    <asp:button id="UploadBtn" Text="Upload Me!" OnClick="UploadBtn_Click" runat="server"></asp:button>
    </form>


    </body>
    </html>


  • Registered Users Posts: 339 ✭✭duffman85


    You're getting those error messages because you've used these controls twice(with the exact same names):

    [PHP]
    protected System.Web.UI.WebControls.Button UploadBtn;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.HtmlControls.HtmlInputText txtImgName;
    protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;[/PHP]

    - once in the code-behind and once on the .aspx page.

    You only declare them in the code behind file if your going to add them to your page programmatically.

    Otherwise just use the <asp:controlname .../> tags to place them on the page. You will still be able to access them in your uploadimages method etc. in the codebehind file.


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    duffman85 wrote: »
    You're getting those error messages because you've used these controls twice(with the exact same names):

    [PHP]
    protected System.Web.UI.WebControls.Button UploadBtn;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.HtmlControls.HtmlInputText txtImgName;
    protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;[/PHP]

    - once in the code-behind and once on the .aspx page.

    You only declare them in the code behind file if your going to add them to your page programmatically.

    Otherwise just use the <asp:controlname .../> tags to place them on the page. You will still be able to access them in your uploadimages method etc. in the codebehind file.

    I've deleted the four lines of code below and now I'm down to 4 errors for the other 4 parameters, UploadImage, Page_Load, UploadButtonClick, and SaveToDB... I can't just delete these as there are {} after them with lines of code associated with these??? :confused::confused::confused:

    protected System.Web.UI.WebControls.Button UploadBtn;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.HtmlControls.HtmlInputText txtImgName;
    protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    I thought that where you have an event like:

    <asp:button id="UploadBtn" Text="Upload Me!" OnClick="UploadBtn_Click" runat="server"></asp:button>

    In my .aspx page...

    That you had to have an event handler like:

    public void UploadBtn_Click(object sender, System.EventArgs e)
    {
    Do this that and the other on event UploadBtn_Click...

    }

    In my aspx.cs page???

    :confused::confused::confused:


  • Advertisement
  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    I've also removed the namespace DBImages {} as this is apparently not required here... I still have 4 errors but now at least the code is working, but I still need to weed out these 4 errors, also to learn what it is I'm doing wrong....

    Thanks in advance for any help with this...


  • Registered Users Posts: 339 ✭✭duffman85


    Darragh29 wrote: »
    I thought that where you have an event like:

    <asp:button id="UploadBtn" Text="Upload Me!" OnClick="UploadBtn_Click" runat="server"></asp:button>

    In my .aspx page...

    That you had to have an event handler like:

    public void UploadBtn_Click(object sender, System.EventArgs e)
    {
    Do this that and the other on event UploadBtn_Click...

    }

    In my aspx.cs page???

    :confused::confused::confused:
    Yes, that's right but you don't have to declare it again like you did with the 4 lines you deleted.

    so if you have <input id="UploadFile" type="file" runat="server" /> to access it in one of your event handlers e.g UploadBtn_Click etc.

    you just type "UploadFile.<property name goes here>" to get what ever property of the UploadFile you want.

    Think of the .aspx page and .aspx.cs file as one and the same. If you have a control on your page its accessible in the .aspx.cs file automatically.
    Darragh29 wrote: »
    protected System.Web.UI.WebControls.Button UploadBtn;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.HtmlControls.HtmlInputText txtImgName;
    protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;

    From the 4 errors from these lines, it sounds like Visual Web Developer has got confused as I only see one PageLoad,UploadBtn_Click event handler in your code. Maybe try a Rebuild or Clean Build from the Build Menu.

    I hope I'm making a bit of sense and this helps.
    While I've made websites using asp .net and c#, web development isn't my day job.

    Good Luck!:D


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    duffman85 wrote: »
    Yes, that's right but you don't have to declare it again like you did with the 4 lines you deleted.

    so if you have <input id="UploadFile" type="file" runat="server" /> to access it in one of your event handlers e.g UploadBtn_Click etc.

    you just type "UploadFile.<property name goes here>" to get what ever property of the UploadFile you want.

    Think of the .aspx page and .aspx.cs file as one and the same. If you have a control on your page its accessible in the .aspx.cs file automatically.



    From the 4 errors from these lines, it sounds like Visual Web Developer has got confused as I only see one PageLoad,UploadBtn_Click event handler in your code. Maybe try a Rebuild or Clean Build from the Build Menu.

    I hope I'm making a bit of sense and this helps.
    While I've made websites using asp .net and c#, web development isn't my day job.

    Good Luck!:D

    Hi Duffman, I tried that, copying and pasting the lines of code into a new website build. I'm so fu*king sick of asp/net and c#, endless stupid compiler problems that just appear out of nowhere. :mad::mad::mad: All I want to do is display/render an image that is stored on my database, in a webpage and you'd want a f*cking PhD in software to do it.


  • Moderators, Science, Health & Environment Moderators Posts: 8,956 Mod ✭✭✭✭mewso


    Did you try to cs file I sent you? Worked for me fine. The other option is to create your own page, copy just the bits in between the form tags on the page you got from another site. Double click the button and paste the code into the button click event it creates for you and so on.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Are you using VS2008? If so it could be that the designer part of the page hasn't refreshed itself.


  • Advertisement
  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    musician wrote: »
    Did you try to cs file I sent you? Worked for me fine. The other option is to create your own page, copy just the bits in between the form tags on the page you got from another site. Double click the button and paste the code into the button click event it creates for you and so on.

    Just sent you a pm on this...


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    John_Mc wrote: »
    Are you using VS2008? If so it could be that the designer part of the page hasn't refreshed itself.

    Hi John,

    I'm using VWD 2005...


Advertisement