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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:17:51+00:00 2026-06-14T17:17:51+00:00

Why am I getting this error, and how can I fix it? ADO.NET code:

  • 0

Why am I getting this error, and how can I fix it?

ADO.NET code:

myConnection.Open();

string cmdStr = "SELECT COUNT(*) FROM Sale WHERE Date = '" + DateTime.Today + "' AND User = '" +     UserBox.Text + "'";

SqlCommand Pexist = new SqlCommand(cmdStr, myConnection3);

int P = Convert.ToInt32(Pexist.ExecuteScalar().ToString());

myConnection.Close();

Error:

Conversion failed when converting date and/or time from character string.

  • 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-06-14T17:17:53+00:00Added an answer on June 14, 2026 at 5:17 pm

    You get this error because you don’t use parameters.

    string cmdStr = "SELECT COUNT(*) FROM Sale WHERE Date = @dt " + 
                     "AND User = @usr " +     
                     "AND Item = @itm " + 
                     "AND Name = @name";
    
     SqlCommand Pexist = new SqlCommand(cmdStr, myConnection3);
     Pexist.Parameter.AddWithValue("@dt", DateTime.Today);
     Pexist.Parameter.AddWithValue("@usr", UserBox.Text);
     Pexist.Parameter.AddWithValue("@itm", ID505.Text);
     Pexist.Parameter.AddWithValue("@name", DropDownList1.SelectedItem.ToString());
     object result = Pexist.ExecuteScalar();
     int P = (result == null ? 0 : Convert.ToInt32(result));
     myConnection.Close();
    

    When you use parameters, you let the database engine treat your input string. The database engine knows how to handle date, string and numbers. You are free from parsing problems and you avoid Sql Injection Attacks.

    For example, what happen in your query string if the textbox ID505 contains a single quote?

    Also, ExecuteScalar returns an object that is

    The first column of the first row in the result set, or a null
    reference (Nothing in Visual Basic) if the result set is empty

    So it is better to check for null before trying to convert the result in an integer.

    EDIT Looking back at this question after a while I should add that in this particular case the return from ExecuteScalar could never be null. This is the case because the query uses the aggregate function COUNT(*) that will return always a valid number also in case of no record found.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting this error in the log.... What can I do to fix this?
Can someone please explain why I'm getting this error Type mismatch: cannot convert from
I want to fix an error of thread 1 EXC_BAD_ACCESS(code=2,address=0x30) i m getting this
I've been getting this error, and I can't figure out how to fix it:
I'm getting this error when building my project, and I can't fix it. I
Not sure why I'm getting this error and can't figure it out? The cycle
In my application I am getting this error: You can only have one <head
I can't figure out why I'm getting this error. I only have one class
Can anyone explain why I'm getting this compile error? Duplicate 'Rad.Core.Aop.MethodArgumentValidation' attribute E:\Scripting\Rad.Core\Properties\AssemblyInfo.cs This
Trying to install pear package and keep getting this strange error. Can you shed

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.