views:

145

answers:

1

I have a problem executing a stored procedure in Silverlight 4/RIA. The only value I get back is null. Am I doing my client and server side code wrong?

Client Side :

public ZipCodesDomainContext _ZipcodesDomainContext = new ZipCodesDomainContext();

        /// <summary>
        /// Creates a new <see cref="MainPage"/> instance.
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
            this.loginContainer.Child = new LoginStatus();
            LoadCity();
        }
private void LoadCity()
        {
            txtCity.Text = _ZipcodesDomainContext.GetCityByZip(42071).Value;
        }

Domain Service :

public string GetCityByZip(int pZip)
        {
            return ObjectContext.sp_GetCityByZip(pZip).ToString();
        }

Data Model (When selecting the stored proc) : alt text

Stored Procedure :

USE [ZIPCODES]
GO
/****** Object:  StoredProcedure [dbo].[sp_GetCityByZip]    Script Date: 08/23/2010 13:48:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_GetCityByZip] 
    @ZIP int

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT City FROM ZipCodes WHERE Zip = @ZIP
END
A: 

What do you mean you are getting null back?

Firstly, your function GetCityByZip returns a string which you are doing a .Value on which isn't valid.

If your getting null back from the stored proc:

return ObjectContext.sp_GetCityByZip(pZip).ToString();

would fail because you would be trying to do a ToString() on a null object.

What is ObjectContext? Are you using Linq To SQL? If so you get back result set which should have a wrapper of all the data so you would do something like:

ObjectContext.sp_GetCityByZip(pZip).City;

And then your other call would just be:

txtCity.Text = _ZipcodesDomainContext.GetCityByZip(42071);

If you clear it up a bit I might be able to figure out what the exact issue is but in it's current state your question has me confused.

Kelsey
-The return statement was returning null is what i meant about getting null back.-ObjectContext is what is used in the other Domain Context functions-I will try adding the '.City'.Thank you for the quick reply! Sorry to you and all for the confusing question, still a newbie to RIA services and LINQ.
Redburn
@Redburn I haven't used RIA so you could have it all right, I was just commenting based on what I was able to decipher from the question. :)
Kelsey
My answer: http://forums.silverlight.net/forums/p/197644/460711.aspx
Redburn