.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Showing picture of an property/product while the property/product is having multiple pictures

Posted By:ASPEvil       Posted Date: July 31, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

How we can show a single image of a item using ASP.Net while it is having multiple images, with the database.
 

Suppose, you are having two database tables, one is: "property_details" and another one is: "property_images".

"property_details" table is for only to store core property values and "property_images" table is to store images for all the properties.

"property_details" is having the following fields:-
-------------------------------
refno
property_title
financial_information
financial_information_type
-------------------------------

With the above mentioned fields of the "property_details" table, "refno" is the field which is used store unique id for each property.
"property_title" is the field which is used to store property title for each property.
"financial_information" is the field to store financial figures; like: 1200, 5000, 15000 etc.
"financial_information_type" is to store currency values; like: INR,USD,GBP etc.

"property_images" is having the following fields:-
-------------------------
refno
pathtouse
-------------------------

With the above mentioned fields of the "property_images" table, "refno" is the field which is used store unique id for each property and these ids are same as the ids of "property_details" table's same field. And to keep this id values same on both of these fields, whenever new a new property is inserted, that time, a unique property is generated and stored with the "property_details" table and that same unique is stored with the "property_images" table too.
"pathhouse" is the field which is used to store the image url of each property.


Now we are having our properties with their own images. Now we have to fetch a single image for each of these properties and other related information for those properties from the "property_details" table.

To do this, I brought a GridView on the ASP.Net webpage and then put the following code with event of the required control(with which event of a control we are wanting to display this gridview with the required data). I used this code to page_load event of the ASP.Net webpage, as follow:-
-------------------------------
[vb.net code]

If Not Page.IsPostBack = True Then
          
        
        
            Dim da As New SqlDataAdapter("SELECT refno,property_title,financial_information,financial_information_type FROM property_details", con)
            Dim ds As New DataSet
            da.Fill(ds, "datas")
            
            If ds.Tables.Count > 0 Then
                If ds.Tables(0).Rows.Count > 0 Then
              
                    ds.Tables(0).Columns.Add("ImagePATH")
            
                    Dim ds2 As New DataSet
            
                    For a As Integer = 0 To ds.Tables(0).Rows.Count - 1
            
                                                
                        Dim da2 As New SqlDataAdapter("SELECT top 1 [pathtouse] FROM property_images where refno='" & ds.Tables(0).Rows(a)(0) & "'", con)
                        da2.Fill(ds2, "pictures")
                          
            
           
                        
                        ds.Tables(0).Rows(a)(4) = ds2.Tables(0).Rows(a)(0)
                        
        
                    Next
                    GridView1.DataSource = ds.Tables(0)
                    GridView1.DataBind()
                        
                End If
                
            End If
            
        End If

-------------------------------


[c# code]
----------------
if (!(Page.IsPostBack == true)) {
    SqlDataAdapter da = new SqlDataAdapter("SELECT refno,property_title,financial_information,financial_information_type FROM property_details", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "datas");
    if ((ds.Tables.Count > 0)) {
        if ((ds.Tables[0].Rows.Count > 0)) {
            ds.Tables[0].Columns.Add("ImagePATH");
            DataSet ds2 = new DataSet();
            for (int a = 0; (a 
                        <= (ds.Tables[0].Rows.Count - 1)); a++) {
                SqlDataAdapter da2 = new SqlDataAdapter(("SELECT top 1 [pathtouse] FROM property_images where refno=\'" 
                                + (ds.Tables[0].Rows[a][0] + "\'")), con);
                da2.Fill(ds2, "pictures");
                ds.Tables[0].Rows[a][4] = ds2.Tables[0].Rows[a][0];
            }
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
    }
}

----------------

With the above code, I fetched all the property data from the table: "property_details" and put those data to the dataset: "ds". Now I made a for loop for each record in the dataset: "ds" and for each record, I used the "refno" field of each of these records and used it with an another select statement which fetch image url: "pathtouse" for each of these records and then put these image url with a new dataaset: ds2 and then add this iame field to the dataset: "ds", with the code: "ds.Tables(0).Rows(a)(4) = ds2.Tables(0).Rows(a)(0)". And since a new field(image url: "pathetouse") is getting added with the dataset: "ds", so its total field count will be increased by 1 and for this, I already added an extra column with the dataset: "ds", by the following code: "ds.Tables(0).Columns.Add("ImagePATH")", before I add new image url field("pathtouse") to it.

So we are now having all the property data from the table: "property_details" with a single image with each property, fetched from the table: "property_images".

Then I put the value of the dataset with the GridView by this code: "GridView1.DataSource = ds.Tables(0)
                    GridView1.DataBind()
"

Now we are having a GridView with the data we wanted.

Now, what will happen if at the time of inserting data for a new property, you had not selected its images. Then the above mentioned code will definitely raise error. So to avoid this situation, I used a code to check if for a particular property, there is no iamge and if it will be the case, then the code will put a default image for that particular property and then that default image will be shown with the GridView. This code is as follow:-
------------------------
[vb.net code]

If Not Page.IsPostBack = True Then
            Dim da_check_blank As New SqlDataAdapter("select refno from property_details where not exists (select refno from property_images where property_details.refno = property_images.refno and refno IS  NOT NULL)", con)
            Dim ds_check_blank As New DataSet
            da_check_blank.Fill(ds_check_blank, "checkingBlank")
            
            If ds_check_blank.Tables.Count > 0 Then
                If ds_check_blank.Tables(0).Rows.Count > 0 Then
            
                    For a As Integer = 0 To ds_check_blank.Tables(0).Rows.Count - 1
                        Try
                            con.Open()
                        Catch
                        End Try
                        Dim cmd_images_nopic_blank As New SqlCommand("insert into property_images values('" & ds_check_blank.Tables(0).Rows(a)(0) & "','~/images/no_image.jpg')", con)
                        cmd_images_nopic_blank.ExecuteNonQuery()
                        con.Close()
                    Next
                    
                End If
            End If
        End If
------------------------

[c# code]
----------
if (!(Page.IsPostBack == true)) {
    SqlDataAdapter da_check_blank = new SqlDataAdapter("select refno from property_details where not exists (select refno from property_images where property" +
        "_details.refno = property_images.refno and refno IS  NOT NULL)", con);
    DataSet ds_check_blank = new DataSet();
    da_check_blank.Fill(ds_check_blank, "checkingBlank");
    if ((ds_check_blank.Tables.Count > 0)) {
        if ((ds_check_blank.Tables[0].Rows.Count > 0)) {
            for (int a = 0; (a 
                        <= (ds_check_blank.Tables[0].Rows.Count - 1)); a++) {
                try {
                    con.Open();
                }
                catch (System.Exception End) {
                    try {
                        SqlCommand cmd_images_nopic_blank = new SqlCommand(("insert into property_images values(\'" 
                                        + (ds_check_blank.Tables[0].Rows[a][0] + "\',\'~/images/no_image.jpg\')")), con);
                        cmd_images_nopic_blank.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
        }
    }
}

----------


Now all is fine and nicely working.



Thanks.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend