troubleshooting Question

Convert NCHAR to string using c# - WebAPI

Avatar of TechIsaac
TechIsaacFlag for United States of America asked on
C#SQL
5 Comments1 Solution105 ViewsLast Modified:
I have a sql parameter which is NCHAR

I want to convert the variable to string as you will see in the code block.

It throwing a error when I reference the variable.

"Message": "An error has occurred.",
    "ExceptionMessage": "Pic_Date",
    "ExceptionType": "System.IndexOutOfRangeException",
    "StackTrace": "   at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)\r\n   at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)\r\n   at System.Data.SqlClient.SqlDataReader.get_Item(String name)\r\n   at lob_totals.Models.LobTotals.pic_of_day(List`1 PicDateCaption, String Date_In) in c:\\!web_insight\\API\\projects\\lob-totals-sol\\lob-totals\\Models\\LobTotals.cs:line 318\r\n   at lob_totals.Controllers.LobTotalsController.pic_of_day(String id) in c:\\!web_insight\\API\\projects\\lob-totals-sol\\lob-totals\\Controllers\\LobTotalsController.cs:line 205"
}

What am I doing incorrectly.
This task is a snippet from my WebAPI

Isaac
string Pic_Date { get; set; } 
 string Pic_Caption { get; set; }

 public List<LobTotals> pic_of_day (List<LobTotals> PicDateCaption, string Date_In)
          {

              using (var con = lob_totals.Models.Database.GetConnection())
              {
                  // Find Pic Of Day
                  using (SqlCommand command = new SqlCommand("insight_sel_pic_day", con))
                  {
                      command.CommandType = System.Data.CommandType.StoredProcedure;
                       
                      con.Open();
                       
                      command.Parameters.Add(new SqlParameter("@INpic_date", System.Data.SqlDbType.NChar)).Value = Date_In;
                      using (SqlDataReader rdr = command.ExecuteReader())
                      // Locate Picture
                          if (rdr.HasRows)
                              while (rdr.Read())
                      {
                          LobTotals picData = new LobTotals();
                          picData.Pic_Date = rdr["Pic_Date"].ToString();
                          picData.Pic_Caption = rdr["Caption"].ToString();
                           



                          PicDateCaption.Add(picData);
                          con.Close();

                      }

                      return PicDateCaption;
                  }
ASKER CERTIFIED SOLUTION
TechIsaac
IT Contracting

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros