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# prob, last one I promise!

Options
  • 22-01-2009 7:45am
    #1
    Closed Accounts Posts: 7,097 ✭✭✭


    Hi Folks,

    I have a 3 level drop down list that is working very well so far, on the event change for the last drop down list, it goes off and pulls data from a single recond in my database, dependent upon the choices made using the 3 dropdown lists...

    In my database, I've got pictures in there and now want to read out a single pic for each record in my database...

    Here's my C# code...

    selectSQL = "SELECT * FROM Vehicle_Data WHERE Vehicle_Make = '" + DropDownList1.SelectedItem.Value + "' AND Vehicle_Model = '" + DropDownList2.SelectedItem.Value + "' AND Engine_Type = '" + DropDownList3.SelectedItem.Value + "'";

    SqlCommand cmdy = new SqlCommand(selectSQL, connz);
    cmdy.CommandType = CommandType.Text;

    SqlDataReader myDataReader;
    myDataReader = cmdy.ExecuteReader();

    //DateTime myDateTime1 = new DateTime();
    //myDateTime1 = new DateTime();
    //myDateTime1 = DateTime.Now;


    Label1.Text = string.Format("Vehicle Information: {0} {1}.", make, model);
    Label7.Text = string.Format("Engine Type: {0}.", color);

    while (myDataReader.Read())
    {
    Label2.Text = string.Format("Your Purchase ID is: {0} ", myDataReader["Stock_Code"]);
    Label3.Text = string.Format("Retail Price: €{0:F2}", myDataReader["Retail_Price"]);
    Label4.Text = string.Format("Product Info: {0} ", myDataReader["Product_Description"]);
    Label5.Text = string.Format("Manufacturer: {0} ", myDataReader["Component_Manufacturer"]);
    Label6.Text = string.Format("Timestamp: {0} ", myDateTime1);

    Response.ContentType = myDataReader["img_contenttype"].ToString();
    Response.BinaryWrite((byte[])myDataReader["img_data"]);


    The problem are the two lines in bold directly above. I got code working that will read the image from my database, from here:

    http://www.aspfree.com/c/a/ASP.NET/Retrieving-Images-from-a-Database--C---Part-II/

    But I have to manually set the following line of code...

    <img src="viewimage.aspx?img=1" border=1> from that script, to make the integer 1 above equal the primary key for whatever record I want to view the image for. This article is just really a code for testing your database image I think...

    What I want to do is get the image to display along with the text that comes from the database when the dropdown list is used, but sticking in the code above in bold hasn't done the job and is throwing up an error. Also, I'm not sure how to tie my c# code in with an image control on my aspx page...

    The errror coming up is: sys.webforms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to response.Write(), response filters, HTTP modules, or server trace is enabled. details: Error parsing near '[][][]JFIF'...

    I'm hoping this might make sense to someone and be an easy fix!

    That's it, when I have this sorted, I won't defile asp.net or c# with my presence any further! :confused::confused::confused:


Comments

  • Registered Users Posts: 2,931 ✭✭✭Ginger


    The issue in this case is that you are doing mixed content types..

    Seperate the code for the image into a seperate ASPX file so that it will only return the image data...

    then you can embed that in your code behind by using an <asp:Image> and setting the ImageURL property to your ASPX file and and ID for the image you want to return


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


    Ok, I have the code in a separate file now and its running on my main page in conjunction with (but not connected or hooked up to, my 3 lavel dropdown list, as I want it to be). I have this line of code in my aspx page controlling what image is rendered into my aspx page...

    <img src="ViewImage.aspx?img=52" width="300" height="200" alt="image" />

    The whole idea behind putting the cs code for this function in with the cs code for my dropdown list was to allow the image file to be pulled with the query for the data from the database in my aspx page, which apparently I can't do now as per the post above by Ginger... Can anyone tell me what I need to replace the 52 above with, to get these images changing dynamically with the data from the dropdown list in a separate page???


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Easy enough...

    One way is the following

    Delete your image tag completely and change it to

    <asp:image ID="imgWhatever" runat="server" />

    When you have figured out your image to show from your dropdowns like in the first code you showed, do the following

    imgWhatever.ImageUrl = "ViewImage.aspx?img=" + myDataReader["Stock_Code"].ToString();

    What this will do is set the value of the Image to the ASPX file and pass in the correct value for you..

    There are other ways to do this as well, but this is possibly the easiest for you


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


    Ginger wrote: »
    Easy enough...

    One way is the following

    Delete your image tag completely and change it to

    <asp:image ID="imgWhatever" runat="server" />

    When you have figured out your image to show from your dropdowns like in the first code you showed, do the following

    imgWhatever.ImageUrl = "ViewImage.aspx?img=" + myDataReader["Stock_Code"].ToString();

    What this will do is set the value of the Image to the ASPX file and pass in the correct value for you..

    There are other ways to do this as well, but this is possibly the easiest for you

    This will work if the image is stored on the server hard drive, but not if its stored in the DB. I always do the latter and find the above method works well every time.


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


    Ginger wrote: »
    Easy enough...

    One way is the following

    Delete your image tag completely and change it to

    <asp:image ID="imgWhatever" runat="server" />

    When you have figured out your image to show from your dropdowns like in the first code you showed, do the following

    imgWhatever.ImageUrl = "ViewImage.aspx?img=" + myDataReader["Stock_Code"].ToString();

    What this will do is set the value of the Image to the ASPX file and pass in the correct value for you..

    There are other ways to do this as well, but this is possibly the easiest for you

    Thanks a mil Ginger. I'm after complicating this one bit further though!

    I changed my DB so that I now have 2 tables...

    Table: Vehicle_Data

    Holds data information on my parts inventory...

    Table: StockImage_Data

    Holds image data for items in the Vehicle_Data table...

    The img_name in my StockImage_Data table is the same value as Stock_ID in my Vehicle_Data table, so I can use a SELECT FROM StockImage_Data WHERE img_name = @MyStockID... Or something like that, if I can pass the data read from Stock_ID to a variable named MyStockID... :confused::confused::confused:


  • Advertisement
  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Yup..

    THe example above.. just shows you passing a value to your ASPX file

    If you use in the ViewImage.aspx file

    in the Page_Load event

    int StockKey = Int32.Parse(Request.QueryString["img"].ToString());

    //Get data yadda yadda

    Response.ContentType = "image/jpeg" //get correct mime type from internet think it should be ok
    Response.BinaryWrite((byte[])myDataReader["img_data"]);


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


    Darragh29 wrote: »
    Thanks a mil Ginger. I'm after complicating this one bit further though!

    I changed my DB so that I now have 2 tables...

    Table: Vehicle_Data

    Holds data information on my parts inventory...

    Table: StockImage_Data

    Holds image data for items in the Vehicle_Data table...

    The img_name in my StockImage_Data table is the same value as Stock_ID in my Vehicle_Data table, so I can use a SELECT FROM StockImage_Data WHERE img_name = @MyStockID... Or something like that, if I can pass the data read from Stock_ID to a variable named MyStockID... :confused::confused::confused:

    You can join the two tables in a single SQL query on Vehicle_Data.img_name=StockImage_Data.ID (or whatever the ID column is called).


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


    This is startin to look a bit hairy! ;) Thanks lads for the help with this, I know now I should have been paying more attention back in uni when doing that C module!


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Not at all.. just fairly simple stuff really... just step back and look at what you need to do


Advertisement