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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:07:10+00:00 2026-05-26T12:07:10+00:00

This is my code: This is in a different class named DBAccess public DataSet

  • 0

This is my code:

This is in a different class named DBAccess

public DataSet getRecords(DateTime dtpFloor,DateTime dtpCeiling)
{

   if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }

   SqlCommand newCmd = conn.CreateCommand();

   newCmd.Connection = conn;

   newCmd.CommandType = CommandType.Text;

   newCmd.CommandText = " SELECT * FROM dbo.ClientInvoice  WHERE invDate BETWEEN  '" + dtpCeiling + "' AND  '" + dtpFloor + "'";

            SqlDataAdapter da = new SqlDataAdapter(newCmd);

            DataSet dsIncome = new DataSet();

            da.Fill(dsIncome, "Client");

             conn.Close();

            return dsIncome;

}

Below Coding is in the ProfitLos form class

public void btnClickFillGrid()
{

    DataSet dsIncome =     dba.getRecords(dtpFloor.Value.ToString(),    dtpCeiling.Value.ToString()); //dba is an object of DBAccess class

    dgvproIncome.DataSource = dsIncome.Tables["Client"].DefaultView;

}

btnClickFillGrid() will invoke at the button click event.

In the database – invdate datetime;(invDate is the variable name and its in the datetime format)

i edited my coding like this

public DataSet getRecords(DateTime dtpFloor,DateTime dtpCeiling)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=KOSHITHA-PC;Initial Catalog=ITP;Integrated Security=True"))
            {
                 conn.Open();
                 using (SqlCommand command = conn.CreateCommand())
                 {
                     string sql = "SELECT * FROM dbo.ClientInvoice WHERE invDate BETWEEN" + "@from AND @to"; 
            command.CommandText = sql;
            command.Parameters.AddWithValue("@from",dtpFloor);
            command.Parameters.AddWithValue("@to", dtpCeiling);

            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet dataSetClient = new DataSet();
            da.Fill(dataSetClient, "Client");
            return dataSetClient;
                }
            }
        }


DataSet dataSetClient = dba.getRecords(dtpFloor.Value, dtpCeiling.Value);
                dgvproIncome.DataSource = dataSetClient.Tables["Client"].DefaultView;

now i m getting an exception in “da.Fill(dataSetClient, “Client”);” line saying
sqlException was unhandled
An expression of non-boolean type specified in a context where a condition is expected, near ‘BETWEEN@from’.

i m not familiar with the parameter passing method to sql query,so couldnt find the problem that i m having

  • 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-26T12:07:11+00:00Added an answer on May 26, 2026 at 12:07 pm

    Look at this call:

    dba.getRecords(dtpFloor.Value.ToString(), dtpCeiling.Value.ToString());
    

    That’s clearly passing in strings as the arguments. Now look at your method declaration:

    public DataSet getRecords(DateTime dtpFloor,DateTime dtpCeiling)

    Those parameters are of type DateTime, not string. So the first thing to fix is the call, to:

    dba.getRecords(dtpFloor.Value, dtpCeiling.Value);
    

    Now the next problem is that you’re embedding the values in the SQL directly. Don’t do that. Never do that. In some cases it can lead to SQL injection attacks, and in other cases it causes data conversion issues (as you’ve got here). Use parameterized SQL instead – oh, and use connection pooling rather than trying to use a single connection in multiple places:

    public DataSet GetRecords(DateTime dtpFloor,DateTime dtpCeiling)
    {
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            using (SqlCommand command = conn.CreateCommand())
            {
                string sql = "SELECT * FROM dbo.ClientInvoice WHERE invDate BETWEEN "
                           + "@from AND @to";
                command.CommandText = sql;
                command.Parameters.AddWithValue("@from", dtpFloor");
                command.Parameters.AddWithValue("@to", dtpCeiling");
    
                SqlDataAdapter da = new SqlDataAdapter(command);
                DataSet dataSet = new DataSet();
                da.Fill(dataSet, "Client");
                return dataSet;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
I am using this code in wordpress to display different content when different pages
If I wrote this code: typeof(myType).TypeHandle Would it use reflection? How much different from:
If I run this code, will each AppDomain execute in a different thread? ThreadPool.QueueUserWorkItem(delegate
I try to draw some lines with different colors. This code tries to draw
This is pretty much the same problem i have, except with very different code:
This questions involves 2 different implementations of essentially the same code. First, using delegate
I have the following code Criteria criteria = this.getCriteriaForClass(DeviceListItem.class); Projection rowCountProjection = Projections.countDistinct(color); criteria.setProjection(rowCountProjection);
I am designing an application where a class named Rights is used. This class

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.