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 6552033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:24:37+00:00 2026-05-25T12:24:37+00:00

I am trying to get the data from database by using the below code…..

  • 0

I am trying to get the data from database by using the below code…..

if there is no data in the table it will always goes to
this statement

I am using mysql.net connector for getting the data and i am doing winforms applications
using c#

     public DataTable sales(DateTime startdate, DateTime enddate)
     {
         const string sql = @"SELECT memberAccTran_Source as Category, sum(memberAccTran_Value) as Value
                              FROM memberacctrans
                              WHERE memberAccTran_DateTime BETWEEN @startdate AND @enddate
                              GROUP BY memberAccTran_Source";

         return sqlexecution(startdate, enddate, sql);
     }

and the below code is for return sqlexceution…function..

 private static DataTable sqlexecution(DateTime startdate, DateTime enddate, string sql)
 {
         var table = new DataTable();
         using (var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring))
         {
             conn.Open();

             var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn);

             var ds = new DataSet();

             var parameter = new MySql.Data.MySqlClient.MySqlParameter("@startdate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter.Direction = ParameterDirection.Input;
             parameter.Value = startdate.ToString(dateformat);
             cmd.Parameters.Add(parameter);

             var parameter2 = new MySql.Data.MySqlClient.MySqlParameter("@enddate", MySql.Data.MySqlClient.MySqlDbType.DateTime);
             parameter2.Direction = ParameterDirection.Input;
             parameter2.Value = enddate.ToString(dateformat);
             cmd.Parameters.Add(parameter2);

             var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);

             da.Fill(ds);
             try
             {
                 table = ds.Tables[0];

             }
             catch
             {
                 table = null;
             }
         }
         return table;
     }

even if there is no data the process flow will goes to this line

table = ds.Tables[0];

how can i reduce this …..

would any one pls help on this….

  • 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-25T12:24:37+00:00Added an answer on May 25, 2026 at 12:24 pm

    In your case if you are think that catch block will get excuted if there is no row available than you are wrong because Even if there is no data once select query is get exucuted without exception it Creates datatable with the columns but with no rows.

    for this i think you can make use of ds.table[0].rows.count property which return 0 if there is no row in datatable.

    if ( ds.Tables[0].Rows.Count > 0 )
         table = ds.Tables[0];
    else
         table=null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example I'm trying to get the data from database like: using (ExplorerDataContext context
I'm trying to get character data from www.wowarmory.com using PHP and cURL. The code
I'm trying to get data from database, and echo them on the page using
I'm trying to get data from another database using the OPENDATASOURCE command, but I
I'm trying to get the video data from this youtube playlist feed and add
I am trying to get some form data from POST method. Here's the code
I am trying to rewrite an url with GET-data from a form. This works
I am trying to make some data from database available using XML, I have
I am trying to retrieve some data from a database using jQuery's $.getJSON method
I'm simply trying to get data from two sql server db tables using ado.net

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.