Link to home
Start Free TrialLog in
Avatar of JameMeck
JameMeckFlag for United States of America

asked on

How to use SqlCommand with Silverlight.

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

At first glance, everything looks OK. Are you getting an exception?
Avatar of JameMeck

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of kovilpattiBalu
kovilpattiBalu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial