1

I'm getting below error while saving data into database :

Procedure or function 'AddName' expects parameter '@CountryName', which was not supplied.

I'm trying to store a value from drop-down selected value text into database. Note that I'm getting the error after data saved into database.

Using #Id I'm able to store Id of country, state & city, but I want to store name of the country, state & city, how to fix the error.

My Stored Procedure:

Create procedure [dbo].[AddName]    
(    
    @CountryName varchar(100),
    @StateName varchar(100),
    @CityName varchar(100)
)    
as    
begin    
   Insert into DropdownName values(@CountryName, @StateName, @CityName)    
End
$("#btnSave").click(function() {
  if ($("#CountryId").val() != "" && $("#StateId").val() != "" && $("#CityId").val() != "") {

    $.ajax({
      type: "POST", //HTTP POST Method
      url: "Home/Index", // Controller/View
      data: { //Passing data
        CountryName: $("#CountryId option:selected").text(), //Reading text box values using Jquery
        StateName: $("#StateId option:selected").text(),
        CityName: $("#CityId option:selected").text(),
        CountryId: $("#CountryId").val(),
        StateId: $("#StateId").val(),
        CityId: $("#CityId").val()
      },
      success: function() {
        alert('Data saved');
      },
      error: function() {
        alert('Error occurred');
      }
    });
  }
});
@using (Html.BeginForm("Index", "Home", FormMethod.Post)) 
{ 
  @Html.DropDownListFor(m => m.CountryId, Model.Countries, "Please select")
  <br />
  <br /> 
  @Html.DropDownListFor(m => m.StateId, Model.States, "Please select")
  <br />
  <br /> 
  @Html.DropDownListFor(m => m.CityId, Model.Cities, "Please select")
  <br />
  <br />
  <input type="submit" value="Submit" id="btnSave" /> 
}
[HttpPost]
public ActionResult Index(CascadingModel model)
{
  AddDetails(model);
  return View(model);
}

public void AddDetails(CascadingModel obj)
{
  string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
  using (SqlConnection con = new SqlConnection(constr))
  {
    SqlCommand com = new SqlCommand("AddName", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@CountryName", obj.CountryName);
    com.Parameters.AddWithValue("@StateName", obj.StateName);
    com.Parameters.AddWithValue("@CityName", obj.CityName);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
  }
}

Model:

public class CascadingModel
{
  public CascadingModel()
  {
    this.Countries = new List<SelectListItem>();
    this.States = new List<SelectListItem>();
    this.Cities = new List<SelectListItem>();
  }

  public List<SelectListItem> Countries { get; set; }
  public List<SelectListItem> States { get; set; }
  public List<SelectListItem> Cities { get; set; }

  public int CountryId { get; set; }
  public int StateId { get; set; }
  public int CityId { get; set; }

  public string CountryName { get; set; }
  public string StateName { get; set; }
  public string CityName { get; set; }
}
5
  • Have you checked to ensure that CountryName has a value at each stage? Ie. before sending the AJAX request, when receiving the request and when supplying the value to the SP? Commented Nov 8, 2019 at 10:27
  • I'm getting value available till the connection string , but after connection string it becomes null Commented Nov 8, 2019 at 10:58
  • 1
    @DineshGaud please change in button type="submit" to "button" Commented Nov 8, 2019 at 12:04
  • @jishansiddique it worked, thanks :) Commented Nov 9, 2019 at 6:46
  • Welcome man :) :) Commented Nov 9, 2019 at 8:15

1 Answer 1

1

Please add this in you cshtml view.

Note: in your view you have set input type="submit" it means submit form with reload page and also you have manage jquery event calling ajax method.

cshtml view

@using (Html.BeginForm("Index", "Home", FormMethod.Post)) 
{ 
  @Html.DropDownListFor(m => m.CountryId, Model.Countries, "Please select")
  <br />
  <br /> 
  @Html.DropDownListFor(m => m.StateId, Model.States, "Please select")
  <br />
  <br /> 
  @Html.DropDownListFor(m => m.CityId, Model.Cities, "Please select")
  <br />
  <br />
  <input type="button" value="Submit" id="btnSave" /> 
}

Jquery Code

$("#btnSave").click(function() {
  if ($("#CountryId").val() != "" && $("#StateId").val() != "" && $("#CityId").val() != "") {

    $.ajax({
      type: "POST", //HTTP POST Method
      url: "@Url.Action("Index", "Home")", // Controller/View
      data: { //Passing data
        CountryName: $("#CountryId option:selected").text(), //Reading text box values using Jquery
        StateName: $("#StateId option:selected").text(),
        CityName: $("#CityId option:selected").text(),
        CountryId: $("#CountryId").val(),
        StateId: $("#StateId").val(),
        CityId: $("#CityId").val()
      },
      success: function() {
        alert('Data saved');
      },
      error: function() {
        alert('Error occurred');
      }
    });
  }
});

Controller Code

[HttpPost]
public ActionResult Index(CascadingModel model,FormCollection fomr)
{
  //also check FormCollection  data

  AddDetails(model);
  return View(model);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.