Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7405197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:21:58+00:00 2026-05-29T05:21:58+00:00

I have a C# app that is reading data from a stored procedure and

  • 0

I have a C# app that is reading data from a stored procedure and then creates a message. I am using SqlDataReader to read the information from the database. Previously, I have had to create a new class every time I have a different type of message and it is getting out of control. By different, I mean a different number of columns and/or different types of data each of the columns contain. I am trying to make it easier to add different message types and not sure how to dynamically format the data as I read it. I have tried to format the data after it has been read into my Data# variables, but it its just junk because it gets formatted as the wrong type. So, the only way I currently see it to handle the data when I read into my variables and I can’t figure out how.

My format of my message is as follows:

Message
   Subject
   (Text)
     MessageHeader1
     MessageHeader2
     Title
     Header1
     Header2
     Header3
     (Data)
        Data1
        Data2
        Data3

The issue is that the Data# variables could contain different type of information. It could currently contain strings, integers, Dates, Percents, etc. Is there a way to format the data based on another value or some other method so that the different type of data could be formatted correctly?

if (dr.Read())
{
    message.Subject = dr["Subject"].ToString();
    message.MessageText = new AlertMessageText()
    {
        MessageHeader1 = dr["MessageHeader1"].ToString(),
        MessageHeader2 = dr["MessageHeader2"].ToString(),
        Title = dr["Title"].ToString(),
        Header1 = dr["Header1"].ToString(),
        Header2 = dr["Header2"].ToString(),
        Header3 = dr["Header3"].ToString(),
        MessageData = new List<AlertMessageData>(),
    };
}

...

while (dr.Read())
{
    message.MessageText.MessageData.Add(new AlertMessageData()
        {
            Data1 = string.Format("{0:n0}", dr["Data1"]),
            Data2 = string.Format("{0:n0}", dr["Data2"]),
            Data3 = string.Format("{0:MM/dd/yyyy}", dr["Data3"]),    // Formats Data as a Date 
        });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:22:00+00:00Added an answer on May 29, 2026 at 5:22 am

    To determine the data type of the value and then apply a specific format, you could do this:

    while (dr.Read())
    {
        var msg = new AlertMessageData();
        for (int i = 1; i < 4; i++)
        {
             var value = dr["Data" + i];
             string format = "{0:n0}";
             if (value is DateTime)
             {
                format = "{0:MM/dd/yyyy}";
             } 
             else if (value is string)
             {
                format = "{0}";
             }      
    
             var stringValue = string.Format(format, value);
             if (i == 1) msg.Data1 = stringValue;
             if (i == 2) msg.Data2 = stringValue;
             if (i == 3) msg.Data3 = stringValue;
        }
        message.MessageText.MessageData.Add(msg);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading data from an IPhone App that uses http POST to transfer
I'm working on an app that is reading it's data from a customized RSS
I have a simple app that reads internet resource and displays the information in
I'm considering using Core Data for an app I'm writing. However, after reading the
I`m working on an app that does reading and handling specific URIs from NFC
Have an app that can use tts to read text messages. It can also
I have a C#.Net application that accesses data from a commercial application backed by
I have asp.net mvc app that shows varios events. All events stored in a
I have an Activity with a list that is bound to a ListAdapter reading
I'm interested in uploading data (images mostly) from my Android App to Amazon S3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.