Click here to Skip to main content
11,436,156 members (64,742 online)
Click here to Skip to main content

Gadget to Fetch Today's Gold Rates (India)

, 10 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Showing current Gold/ Silver rate (India) from webpage.


This is my first article on CodeProject. So if there are any mistakes please forgive me. This Article explains how to fetch today's Gold/ Silver rates (India) from the web. It might not be of use for guys from other than India but, it will help you understand how you can get specific text/ data from webpage.


CodeProject has helped me a lot in my programming. So finally I have decided to contribute to CodeProject.

One of my friend in college has project on "Jewelry System". For this application he came with an idea to show the user today's Gold/ Silver rate in selected city. He came to me and asked if it is possible? At first I laughed at him. but later i thought it will be great if done. So I started Googling for it but found nothing.

So I decided to try it myself and after a week had success!

Using the code

Namespaces required-

  1. To work with Web/ HTTP classes
  2. using System.Net;
  3. To work with Stream
  4. using System.IO;

Function to get Gold/ Silver Rate

Following is the function which takes the URL as input and returns the source of the page. The variable temp is used to get only required data from the page. Substring is a function of String class which takes two arguments. first is Starting Index and second is [Optional] length. Replace is another function of String which replaces specific/ given string with given second parameter string.

public string rates(string urlAddress)
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = null;
            if (response.CharacterSet == null)
                readStream = new StreamReader(receiveStream);
                readStream = new StreamReader(receiveStream, 
            string data = readStream.ReadToEnd();
            temp = data.Substring(data.LastIndexOf("10g"), 30);
            temp = temp.Replace("<TD>", "");
            temp = temp.Replace("</TD>", "");
            temp = temp.Replace("\t", " ");
            temp = temp.Replace("\n", " ");
    catch (Exception) { MessageBox.Show("Cannot connect to the server."); }
    return temp;

ComboBox's SelectionChangeCommited Event

Here I have assigned comboBox Item specific URL to get Gold and Silver rate from the page. And called the function rates with URL as argument.

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    lbl_gold.Text = "Gold: ";
    lbl_silver.Text = "Silver: ";
    string temp = comboBox1.SelectedValue.ToString();
    temp = temp.Substring(0, temp.IndexOf("@"));            
    lbl_gold.Text += rates(temp);
    temp = comboBox1.SelectedValue.ToString();
    temp = temp.Substring(temp.IndexOf("@") + 1);
    lbl_silver.Text += rates(temp);

Close application on Form1_DoubleClick event 

Close the application on Double_Click of the form.

 private void Form1_DoubleClick(object sender, EventArgs e)
            myDictionary = null;

Form Load event  

In this event I have wrote the code to show application at upper right corner of the screen. And checking if there is an working internet connection availability.

private void Form1_Load(object sender, EventArgs e)
    this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - this.Width;
    this.Top = 0;            
    if (!HasConnection())
        label4.Text = "No Internet connection!";
        comboBox1.Enabled = false;
    myDictionary.Add("Delhi", "" + 
    myDictionary.Add("Mumbai", "" + 
    myDictionary.Add("Nashik", "" + 
      "" + 
    myDictionary.Add("Kolkata", "" + 
    myDictionary.Add("Kochi", "" + 
    myDictionary.Add("Hyderabad", "" + 
    myDictionary.Add("Bangalore", "" + 
    myDictionary.Add("Pune", "" + 
    myDictionary.Add("Jaipur", "" + 
    myDictionary.Add("Ahmedabad", "" + 
    comboBox1.DataSource = new BindingSource(myDictionary, null);
    comboBox1.DisplayMember = "Key";
    comboBox1.ValueMember = "Value";


Function to check internet connection

This function checks whether the computer has a working internet connection.

public static bool HasConnection()
        System.Net.IPHostEntry i = System.Net.Dns.GetHostEntry("");
        return true;
        return false;

Points of Interest

This article gives an idea on how you can get specific data from website. I know there is a lot of things can be done. Participation in the project would be greatly appreciated and can be given in any form (direct code contributions, comments, votes, emails and such). The main point is to get ideas going and implement them which is really useful for learning.


  • Initial release (9 April 2013)
  • Code Improve (10 April 2013)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
No Biography provided

Comments and Discussions

QuestionHow to use it in html file Pin
gore nilesh8-Apr-14 2:36
membergore nilesh8-Apr-14 2:36 
GeneralThanks Pin
Member 1023238227-Sep-13 1:34
memberMember 1023238227-Sep-13 1:34 
GeneralRe: Thanks Pin
javedsmart9-Jan-14 0:07
memberjavedsmart9-Jan-14 0:07 
QuestionHi.. Pin
Member 1030123927-Sep-13 1:30
memberMember 1030123927-Sep-13 1:30 
AnswerRe: Hi.. Pin
javedsmart9-Jan-14 0:07
memberjavedsmart9-Jan-14 0:07 
GeneralUseful tip Pin
Sampath Sridhar9-Apr-13 18:58
memberSampath Sridhar9-Apr-13 18:58 
GeneralRe: Useful tip Pin
javedsmart10-Apr-13 5:09
memberjavedsmart10-Apr-13 5:09 
SuggestionSome code refactoring Pin
Ranjan.D9-Apr-13 5:40
memberRanjan.D9-Apr-13 5:40 
GeneralRe: Some code refactoring Pin
javedsmart10-Apr-13 5:20
memberjavedsmart10-Apr-13 5:20 
GeneralRe: Some code refactoring Pin
Ranjan.D10-Apr-13 6:41
memberRanjan.D10-Apr-13 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 10 Apr 2013
Article Copyright 2013 by javedsmart
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid