Adding Interactivity To Charts In Microsoft Visual Studio 2010

Category: ASP.NET

In this tutorial we will use basic SQL knowledge to modify our chart with ease and use it to reflect new data quickly. To complete this tutorial you must have completed the Data Binding A Chart To A Database tutorial.

We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud servers!

Creating The Interface

  1. We are going to start off by dragging out a drop down list in Design view underneath of our chart. Click the Smart Tag on the right and click on Choose Data Source. Since we have already set up a SQL connection you can select SqlDataSource1 from the drop down.
  2. In the SqlDataSource dialog box that appears, select SqlDataSource1 and then move to the bottom of the dialog box and click “Refresh Schema”. After you have done this the two drop down boxes should be populated. In the first down box select “SalesPeople” and in the second choose “id”.
  3. Next we will drop a basic text box and button near the dropdown that will allow us to edit the sales of the Sales person that we select. Give the text box an ID of numTxt and the button an ID of updateSales.
  4. In the properties menu of our drop down box change the ID of the dropdown to personDrop, and set the AutoPostBack property to true.
  5. With the dropdown box selected click on the Events Icon (Lightning Bolt). Double click on the SelectedIndexChange action and it will bring you into the code behind.

Populating Our Text Field

Now that our interface is set up let’s start out by filling in our functions in the code-behind. You should be inside the personDrop_SelectedIndexChanged() function. Paste the following code:

I just signed up at Server Intellect and couldn't be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.


 protected void personDrop_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strSalesName = personDrop.Text;
        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Select SalesLastYear From Sales WHERE ID = @ID", conn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", strSalesName);


        using (conn)
        {
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {
                numTxt.Text = dr["SalesLastYear"].ToString();
            }
        }
    
    }

A Quick Walkthrough This Function

What this function Is doing is populating our numTxt Text field with the sales that the current person in the drop down box has. We do this by connecting to our database, selecting the sales based on the ID, and finally using a SqlDataReader we fill in the numTxt text field.

We chose Server Intellect for its cloud servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Updating Our Chart

In Design view double click our updateSales button. Doing this will bring us into our code behind and place us in the function that we will use to change our chart data, updateSales_Click();. Let us place the following code into updateSales_Click() function:

  
 protected void updateSales_Click(object sender, EventArgs e)
    {
        string strNewSales = numTxt.Text;
        string strSalesID = personDrop.Text;

        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Update Sales Set [email protected] WHERE ID = @ID", conn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@SalesLastYear", strNewSales);
        cmd.Parameters.AddWithValue("@ID", strSalesID);

        using (conn)
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

A Quick Walkthrough This Function

We set our variables to define the new sales number and check to see who the person being edited is by listing the strSalesID variable. We then create our SQL connection and “Update” the database with the new sales number entered in numTxt.Text. Run the application and change some values!

We used over 10 web hosting companies before we found Server Intellect. Our new cloud server,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Success!

As you can see dynamic graphs can be very powerful, these are only a few data entries but imagine having hundreds or more data fields that are constantly changing that you need to show. That is where something such as this would be extremely useful. Congratulations you are now a smarter charter!

Download Source