Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Convert .Net datatype to Mysql datatype

Convert .Net datatype to Mysql datatype

Scheduled Pinned Locked Moved C#
questioncsharpdatabasemysqlhelp
8 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    student1988
    wrote on last edited by
    #1

    Hi people, I have the a form in my application where the user can enter a data and time and then save it to a Mysql database. but I get a FormatExeption saying "Input string was not in a correct format" How can I convert from .net datatime datatype to Mysql database type ? Heres my method for inserting a row that contains a datetime datatype private void shiftInsertNewRow() { conn = new MySqlConnection ("datasource=localhost;username=admin;password=student;database=hospital"); conn.Open(); MySqlCommand command = new MySqlCommand ("insert into shifts(ShiftID, Starttime, Endtime, WardID, NurseID) select ?ShiftID, ?Starttime, ?Endtime, ?WardID, ?NurseID", conn); command.Parameters.Add(new MySqlParameter("?ShiftID", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("?Starttime", MySqlDbType.Datetime)); command.Parameters.Add(new MySqlParameter("?Endtime", MySqlDbType.Datetime)); command.Parameters.Add(new MySqlParameter("?WardID", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("?NurseID", MySqlDbType.Int32)); command.Parameters[0].Value = shiftidTextBox.Text; command.Parameters[1].Value = startTimeTextBox.Text; command.Parameters[2].Value = endTimTextBox.Text; command.Parameters[3].Value = wardShiftidTextBox.Text; command.Parameters[4].Value = wardNameTextBox.Text; command.ExecuteNonQuery(); conn.Close(); } any help would be greatly appreciated :)

    C 1 Reply Last reply
    0
    • S student1988

      Hi people, I have the a form in my application where the user can enter a data and time and then save it to a Mysql database. but I get a FormatExeption saying "Input string was not in a correct format" How can I convert from .net datatime datatype to Mysql database type ? Heres my method for inserting a row that contains a datetime datatype private void shiftInsertNewRow() { conn = new MySqlConnection ("datasource=localhost;username=admin;password=student;database=hospital"); conn.Open(); MySqlCommand command = new MySqlCommand ("insert into shifts(ShiftID, Starttime, Endtime, WardID, NurseID) select ?ShiftID, ?Starttime, ?Endtime, ?WardID, ?NurseID", conn); command.Parameters.Add(new MySqlParameter("?ShiftID", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("?Starttime", MySqlDbType.Datetime)); command.Parameters.Add(new MySqlParameter("?Endtime", MySqlDbType.Datetime)); command.Parameters.Add(new MySqlParameter("?WardID", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("?NurseID", MySqlDbType.Int32)); command.Parameters[0].Value = shiftidTextBox.Text; command.Parameters[1].Value = startTimeTextBox.Text; command.Parameters[2].Value = endTimTextBox.Text; command.Parameters[3].Value = wardShiftidTextBox.Text; command.Parameters[4].Value = wardNameTextBox.Text; command.ExecuteNonQuery(); conn.Close(); } any help would be greatly appreciated :)

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      I would imaging it would work the same was as SQL Server. Convert the text to a DateTime object, then pass the DateTime object as a parameter.

      Man who stand on hill with mouth open wait long time for roast duck to drop in

      S 1 Reply Last reply
      0
      • C Colin Angus Mackay

        I would imaging it would work the same was as SQL Server. Convert the text to a DateTime object, then pass the DateTime object as a parameter.

        Man who stand on hill with mouth open wait long time for roast duck to drop in

        S Offline
        S Offline
        student1988
        wrote on last edited by
        #3

        well I tried to assign a DateTime object to Value but it didnt work either command.Parameters[1].Value = DateTime.Now; command.Parameters[2].Value = DateTime.Now;

        C 1 Reply Last reply
        0
        • S student1988

          well I tried to assign a DateTime object to Value but it didnt work either command.Parameters[1].Value = DateTime.Now; command.Parameters[2].Value = DateTime.Now;

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          student1988 wrote:

          well I tried to assign a DateTime object to Value but it didnt work either

          Define "didn't work". Exactly how did it not work? Did it cause an exception? Did it fail to compile? Did it run but the result was wrong? What?

          Man who stand on hill with mouth open wait long time for roast duck to drop in

          S 1 Reply Last reply
          0
          • C Colin Angus Mackay

            student1988 wrote:

            well I tried to assign a DateTime object to Value but it didnt work either

            Define "didn't work". Exactly how did it not work? Did it cause an exception? Did it fail to compile? Did it run but the result was wrong? What?

            Man who stand on hill with mouth open wait long time for roast duck to drop in

            S Offline
            S Offline
            student1988
            wrote on last edited by
            #5

            it threw the same format exception again.

            L 1 Reply Last reply
            0
            • S student1988

              it threw the same format exception again.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              now define 'it' exceptions hold a lot of information, including exact line numbers. Which statement throws what? the quality and detail of your question reflects on the effectiveness of the help you are likely to get :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              S 1 Reply Last reply
              0
              • L Luc Pattyn

                now define 'it' exceptions hold a lot of information, including exact line numbers. Which statement throws what? the quality and detail of your question reflects on the effectiveness of the help you are likely to get :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                S Offline
                S Offline
                student1988
                wrote on last edited by
                #7

                During runtime when I execute shiftInsertNewRow() method I get a Format exception at this line: command.ExecuteNonQuery() that says input string was not in a correct format.

                L 1 Reply Last reply
                0
                • S student1988

                  During runtime when I execute shiftInsertNewRow() method I get a Format exception at this line: command.ExecuteNonQuery() that says input string was not in a correct format.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  I am not sure, haven't used command parameters like that yet, however I expect the "Values" you provide should come as a string representation that converts automatically to the type you specified, hence: 1. the DateTime parsing will succeed only when the string was formatted according to your default DateTime format, which is a user's preference (see regional settings). 2. an empty textbox will not convert to most simple types; example: int.Parse("") throws an exception! 3. there is an issue about null; when null is intended, it must be specified as DbNull.Value :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups