How to use SqlCommand with Silverlight.

Posted on 2011-02-13
Last Modified: 2013-12-17
I have some labels in my silverlight application, and I want to get data from SQL server for this label text.
How can I do that?

Something's like this with C#:

mySqlConnection = new SqlConnection(ConnectionString);

			SqlDataReader myReader = null;
			string sql = "select isnull(sum(Quantity),0) Quantity from Inventory where [Type] = 'Primary'";
			string sql2 = "select isnull(sum(Quantity),0) Quantity from Inventory where [Type] = 'Secondary'";

			SqlCommand mySqlCommand = new SqlCommand(sql + ";" + sql2, mySqlConnection);

				myReader = mySqlCommand.ExecuteReader();

				while (myReader.Read())
					RemainingPrimary.Text = myReader["Quantity"].ToString();
				 while (myReader.Read())
					RemainingSecondary.Text = myReader["Quantity"].ToString();
			catch (Exception ex)
				MessageBox.Show("Can't Get Data" + ex.Message);

				if (myReader != null)
				if (mySqlConnection.State == ConnectionState.Open)

Open in new window

Question by:JameMeck
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34886744
At first glance, everything looks OK. Are you getting an exception?

Author Comment

ID: 34894770
My application is silverlight.
Those codes for WPF, I cannot run them with silverlight.


Accepted Solution

kovilpattiBalu earned 500 total points
ID: 34896125

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now