0

This my code where my passing dates and I want to display the data from the table as its from sql to view in ASP.NET MVC 5. And that two dates are also comes from view means after selecting this dates I have to pass it into the parameter in stored procedure

public DataTable Get_Availability(string ID = "")
{
  string aDate = "03/03/2017";
  string dDate = "20/03/2017";
  DateTime oDate = Convert.ToDateTime(aDate);
  DateTime uDate = Convert.ToDateTime(dDate);

  queryString = "FO.USP_Check_AvailabilityOFRoom";

  try
  {
     using (SqlConnection conn = new SqlConnection(ConnectionString))
     {
        SqlCommand cmd = new SqlCommand(queryString, conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(New SqlParameter("@Date_Of_Birth",Data.SqlDbType.DateTime));
        cmd.Parameters("@Date_Of_Birth").Value = DOB

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        da.Fill(ds);

        if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
        {
          if (ds.Tables[0].Rows.Count > 0)
          {
             return ds.Tables[0];
          }
        }
     }
  }
  catch
  {
     return null;
  }
  return null;
}
Patrick
  • 5,526
  • 14
  • 64
  • 101
  • Please check your code formatting and add an explanation what the exact problem is at the moment. What is not working? – Sami Kuhmonen Mar 17 '17 at 06:11
  • Check this http://stackoverflow.com/questions/25279547/how-to-display-database-records-in-asp-net-mvc-view?answertab=active#tab-top – J.SMTBCJ15 Mar 17 '17 at 06:14
  • What is the error you are getting can u elaborate it ... You can use parameters like this way "FO.USP_Check_AvailabilityOFRoom '"+"03/03/2017"+"','"+"20/03/2017"+"'" – Raviteja Mar 17 '17 at 06:19
  • return the dataset to view and display the table. there are many ways. go thorugh this link http://stackoverflow.com/questions/20878881/extracting-data-from-dataset-in-view-layer-of-mvc – PRABA Mar 17 '17 at 06:20
  • i have two dates and i want to pass that dates as a parametere to stored procedure and store procedure give table and i want that table e.g. i have arrival date and departure date of hotel i want to pass that dates to the stored procedure – Pooja Shreyas Mar 17 '17 at 06:27
  • Try this http://www.c-sharpcorner.com/UploadFile/db2972/datatable-in-viewdata-sample-in-mvc-day-3/ – Raviteja Mar 17 '17 at 06:30
  • how to pass dates to stored procedure as parameter in above code – Pooja Shreyas Mar 17 '17 at 06:33
  • from where you want to get the dates either user or constant if you want to get it from user pass in method parameters and use jquery call the method – Raviteja Mar 17 '17 at 06:39
  • please give me example – Pooja Shreyas Mar 17 '17 at 06:51
  • problem solve. thank you! cmd.Parameters.Add("@arrDate", oDate) ; cmd.Parameters.Add("@depDate", uDate); – Pooja Shreyas Mar 17 '17 at 06:55
  • Remove commented code in sample. – Patrick Mar 20 '17 at 20:08

1 Answers1

0

Try this

cmd.Parameters.Add("@parametername", date);

@parametername is parameter in you SP.
date is your date variable.

Kay
  • 246
  • 2
  • 16