0

I wanted to get Json data from web method.In here i wanted to get News & Speaker both data but here only working News only.(Unable to get Speaker )

Here is my Stored Procedure

ALTER procedure [dbo].[LoadDayEvents] 
@Date date
as 
begin
select News,Speaker from Eventstbl
where DateToBePublished = CONVERT(date, @Date)
end

Here is my Web Method

 [WebMethod, ScriptMethod]
public static string SelectEventDate(string date)
{
    string News= "";
    string Speaker = "";

    try
    {
        SqlCommand comld = new SqlCommand("LoadDayEvents", conDB);
        comld.CommandType = CommandType.StoredProcedure;
        comld .Parameters.Add("@Date", SqlDbType.Date);
        comld .Parameters["@Date"].Value = DateTime.Parse(date).Date;
        if (conDB.State == ConnectionState.Closed)
            conDB.Open();
        News = comld .ExecuteScalar().ToString();
        Speaker = comld .ExecuteScalar().ToString();  // Thisone is not working
    }
    catch (Exception ee) { }
    finally { conDB.Close(); }
    return News;
}

Here i use Ajax/Json to get it

  <script type="text/javascript">
    $(document).ready(function () {

        var urinews = '<%= ResolveUrl("WebMethods.aspx/SelectEventDate") %>';
        var localtime = new Date();
        var today = localtime.getFullYear() + '/' + (localtime.getMonth() + 1) + '/' + localtime.getDate();


        $.ajax({
            type: "POST",
            url: urinews,
            data: "{ date: '" + today + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (msg) {
                $("#daily_news_Selection").append("<p>" + msg.d + "</p>");
            },
            error: function (x, e) {
            }
        });
    });
</script>
3
  • Your code seems to be partial. Where is the variable Verse defined? Commented Feb 20, 2014 at 16:43
  • Verse? i already remove that. Commented Feb 20, 2014 at 16:45
  • @ajp I wanted to get Speaker name in web method.News is working Commented Feb 20, 2014 at 16:46

1 Answer 1

1

You could return custom class

 var newsAndSpeaker = new NewsAndApeaker() { News = News, Speaker = Speaker}; //or define a custom class that has these two properties
 return newsAndSpeaker;

On Javascript, you can access the two variables using

msg.d.News //news
msg.d.Speaker

Or, you could return a list of string

 var newsAndSpeaker = new List<string>() { News, Speaker}; //or define a custom class that has these two properties
 return newsAndSpeaker;

On Javascript, you can access the two variables as in like a array

msg.d[0] //mews
msg.d.[1] //speaker

If your question is why your second property is not getting populated, then title seems to be wrong

Sign up to request clarification or add additional context in comments.

1 Comment

In the return part it shows the error "newsAndSpeaker" does not exists in the current context

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.