Solved

How to use SqlCommand with Silverlight.

Posted on 2011-02-13
3
1,018 Views
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);

			try
			{
				mySqlConnection.Open();
				myReader = mySqlCommand.ExecuteReader();

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

			}
			finally
			{
				if (myReader != null)
					myReader.Close();
				if (mySqlConnection.State == ConnectionState.Open)
					mySqlConnection.Close();
			}

Open in new window

0
Comment
Question by:JameMeck
3 Comments
 
LVL 75

Expert Comment

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

Author Comment

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

0
 
LVL 7

Accepted Solution

by:
kovilpattiBalu earned 500 total points
ID: 34896125
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

777 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