locked
A null value for parameter 'id' of non-nullable type 'System.Int32' for method 'Test1.Models.Event FormView1_GetItem(Int32)' in 'Test1.EventDetails'. An optional parameter must be a reference type or a nullable type. RRS feed

  • Question

  • User380908829 posted

    I am new at working with web api and getting data from there. 

    this is my error

    A null value for parameter 'id' of non-nullable type 'System.Int32' for method 'Test1.Models.Event FormView1_GetItem(Int32)' in 'Test1.EventDetails'. An optional parameter must be a reference type or a nullable type.

     

    I've got one web application and one web api .

    this is the code for the formview , can somebody tell me how to fix this error !

    .aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="EventDetails.aspx.cs" Inherits="Test1.EventDetails" %>


    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


    <asp:FormView ID="FormView1" runat="server" ItemType="Test1.Models.Event" SelectMethod="FormView1_GetItem" >
    <ItemTemplate>
    <div>
    <h1>
    <%#: Item.title %>
    </h1>
    </div>
    <br />
    <table>
    <tr>
    <td>

    </td>
    <td>&nbsp;</td>
    <td style="vertical-align:top; text-align:left;">
    <b>Description</b><br /><%#: Item.description %><br /><span><b>Event ID</b>&nbsp;<%#: Item.eventkey %></span></td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:FormView>

    </asp:Content>

    and this is 

    aspx,cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net.Http;
    using System.Net;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    using Test1.Models;
    using System.Globalization;
    using Newtonsoft.Json;
    using System.Web.ModelBinding;


    namespace Test1
    {
    public partial class EventDetails : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {



    RunAsync().Wait();

    }

    async Task RunAsync()
    {

    using (var client = new HttpClient())
    {
    client.BaseAddress = new Uri("http://localhost/locator/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    string v = Request.QueryString["eventkey"];
    HttpResponseMessage response = await client.GetAsync("api/Event/getevdetails?id=" + v).ConfigureAwait(false);
    if (response.IsSuccessStatusCode)
    {
    Event myEvent = await response.Content.ReadAsAsync<Event>();

    FormView1.DataSource = myEvent;
    FormView1.DataBind();




    }

    }
    }

    // The id parameter should match the DataKeyNames value set on the control
    // or be decorated with a value provider attribute, e.g. [QueryString]int id
    public Test1.Models.Event FormView1_GetItem([QueryString]int id)
    {

    var uri = new Uri("http://localhost/locator/api/Event/getevdetails?id=" + id);
    using (var client = new WebClient())
    {
    return JsonConvert.DeserializeObject<Event>(
    client.DownloadString(uri)
    );
    }
    }

    }



    }

    // The id parameter should match the DataKeyNames value set on the control
    // or be decorated with a value provider attribute, e.g. [QueryString]int id

    Saturday, February 14, 2015 10:26 AM

Answers

  • User1728944201 posted

    change to look like this:

     var uri = new Uri("http://localhost/locator/api/Event/getevdetails?id=" + Convert.ToString(id));

    • Marked as answer by An0nym0u5User Tuesday, June 22, 2021 12:00 AM
    Monday, February 16, 2015 7:27 AM

All replies

  • User-760709272 posted

    try changing

    public Test1.Models.Event FormView1_GetItem([QueryString]int id)

    to

    public Test1.Models.Event FormView1_GetItem([QueryString]int? id)

    you might have to change some code inside the method too to handle the fact that id might be null.

    Sunday, February 15, 2015 8:16 AM
  • User2008642861 posted

    Hi Drenusha,

    Thank you for your post. I suggest that you could add a "if" statement to prevent if your parameter is empty.

    Please refer to below code.

    public Test1.Models.Event FormView1_GetItem([QueryString]int id)
            {
                if (id.Equals(null))
                {
                    var uri = new Uri("http://localhost/locator/api/Event/getevdetails?id=" + id);
                    using (var client = new WebClient())
                    {
                        return JsonConvert.DeserializeObject<Event>(
                        client.DownloadString(uri)
                        );
                    }
                }
                else 
                {
                    string msg = "";
                    return msg="The parameter is null!";
                }
            }

    Hope this could be helpful to you.

    Best regards,

    Archer

    Sunday, February 15, 2015 10:13 PM
  • User380908829 posted

    Error:  Cannot implicity convert type 'string' to \Test1.Models.Event'

    here is the model that i have:

    public class Event

    {

    public int eventkey {get;set;}

    public int type_fk {get;set;}

    public string title {get;set;}

    }

    :(

    Monday, February 16, 2015 6:50 AM
  • User1728944201 posted

    change to look like this:

     var uri = new Uri("http://localhost/locator/api/Event/getevdetails?id=" + Convert.ToString(id));

    • Marked as answer by An0nym0u5User Tuesday, June 22, 2021 12:00 AM
    Monday, February 16, 2015 7:27 AM
  • User616014865 posted

    I am telling everybody in this blog to put try..catch around Request.QueryString because the parameter you are expecting ain't defined in url string.

    At Catch( ), you should put in a default value for v.

    Monday, February 16, 2015 1:00 PM