Solved

How to use SqlCommand with Silverlight.

Posted on 2011-02-13
3
1,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…

733 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