2 Replies - 3665 Views - Last Post: 05 April 2012 - 06:37 AM Rate Topic: -----

#1 AOM_Set  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 18-December 10

Getting an URL and show the image (C#)

Posted 13 March 2012 - 04:15 PM

Hi
I am trying to create a homepage.
Shortly, my problem is... I have added to images (left and right arrows) to my html page, and when a user clicks on one of these arrows, a GET method occurs (get the next photo, and show it on the htmlpage). I am trying to write the code that connects to the Mysql database, where I have created a table called Photos. The table has 3 columns: "id (int), picturename (varchar) and src (varchar)"
So I'm not saving my photos in the database, I only save references to the pictures. How do I go about with this?
Here is my html code:
 <div>
      <a href="" class="leftArrow"><img id="venstrePil" style="position: relative; top: 350px; left: 0px" 
         src="smallLeftArrow.jpg" alt="" runat="server" /></a>  <!-- This is the left arrow the get's the previous pic -->

      <div class="innerDiv">  
         <img ID="midPic" class="billede" src="" alt="" runat="server"/>  <!--This is where the picture should be shown-->
      </div>



Here is the C# code that connects to the database:
 protected void uploadPic()
    {
        MySqlConnection connection = new MySqlConnection("SERVER=server;PORT=port; DATABASE=database; UID=userId;
                                                          PASSWORD="pw");   
        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT * from Billeder ORDER BY id";

        Uri tempUri = new Uri("http://www.ExampleUri.com");
        String query = tempUri.Query;
        String resName = HttpUtility.ParseQueryString(query).Get("imgnr");     

        try
        {
            connection.Open();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            midPic.Src = ???? I'm stuck here. Notice that midPic is the ID from the html code.

        }
        connection.Close();
    }


I'm pretty lost, so any help is greatly appreciated.
For those who have not understood my problem yet, here is the code that shows the exact concept that I want to do. It is just expressed in php code (I didnt write this code, someone else did it to show me the overall idea). I am not familiar with PhP, but I do understand most of the code below, since I've programmed java a lot (I know these 2 languages are not comparable).

<html>
	<head>
		<title>Billeder</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
</html>
<body>
<?php
$conn = mysql_connect("mysql.domain.net", "domain_net", "password");
mysql_select_db("domain_net_db", $conn);
$imgnr = $_GET['imgnr'];
if ( !ctype_digit($imgnr) ) $imgnr = 0;
$cnt_billeder = mysql_result(mysql_query("SELECT COUNT( * ) FROM Billeder"), 0);
$get_billeder = mysql_query("SELECT * FROM Billeder ORDER BY id ASC LIMIT ".$imgnr.",1");
$go_billeder = mysql_fetch_array($get_billeder);
echo "<img style=\"width: 100px; \" src=\"images/".$go_billeder['src']."\" alt=\"".$go_billeder['billednavn']."\"/>" . "<br />";

$previmg = ($imgnr == 0) ? $cnt_billeder - 1 : $imgnr - 1;
$nextimg = ($imgnr + 1) % $cnt_billeder;

echo "<a href=\"?imgnr=" . $previmg . "\">Forrige</a>";
echo "<a href=\"?imgnr=" . $nextimg . "\">Næste</a>";

?>
</body>
</html>


Thanks alot

Is This A Good Question/Topic? 0
  • +

Replies To: Getting an URL and show the image (C#)

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 162
  • View blog
  • Posts: 565
  • Joined: 03-February 10

Re: Getting an URL and show the image (C#)

Posted 14 March 2012 - 10:44 AM

You should be able to do:

 while (reader.Read())
       {
           midPic.Src = HttpUtility.UrlEncode(reader["src"].ToString());

       }



this all assumes that the source value that is stored in the database is a fully qualified url.
Was This Post Helpful? 1
  • +
  • -

#3 AOM_Set  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 18-December 10

Re: Getting an URL and show the image (C#)

Posted 05 April 2012 - 06:37 AM

View Postdemausdauth, on 14 March 2012 - 10:44 AM, said:

You should be able to do:

 while (reader.Read())
       {
           midPic.Src = HttpUtility.UrlEncode(reader["src"].ToString());

       }



this all assumes that the source value that is stored in the database is a fully qualified url.

Lets say that the src contains 2 URL's (ofcourse each URL has its own column and id):
www.andrew.com/andy.jpg and
www.andrew.com/jane.png

If I want it to open the first url, how will I do this?

What I have tried so far, is:
midPic.Src = HttpUtility.UrlEncode(reader["src"].ToString());



But since we have it in a while loop, it will just continue looping till it reachs the end, even if the user only clicked on the button once (The button that says next or previous, for viewing the image)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1