Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I am facing problem that I want to convert date time format taking date time from database and display date in other format i used system.globalization and deal with DateTime.ParseExact but CultureInfo.InvariantCulture does not identify there is an error:
the best overloaded method match for System.DateTime.ParseExact(string, string, System.IFormatProvider)' has some invalid arguments and can not convert from object to string

so this is full code plz help
C#
void EMPdspunis()
      {
          string dtfrom = "";
          string dtto = "";
          dtfrom = DTPFROM.Value.ToShortDateString();
          dtto = DTPTO.Value.ToShortDateString();
          string mm = DTPFROM.Value.Month.ToString();
          if (mm.Length == 1)
          {

              mm = "0" + mm;
          }
          string dd = DTPFROM.Value.Day.ToString();
          if (dd.Length == 1)
          {
              dd = "0" + dd;

          }
          dtfrom = DTPFROM.Value.Year.ToString() + mm + dd;

          mm = DTPTO.Value.Month.ToString();
          if (mm.Length == 1)
          {
              mm = "0" + mm;
          }
          dd = DTPTO.Value.Day.ToString();
          if (dd.Length == 1)
          {

              dd = "0" + dd;

          }

          dtto = DTPTO.Value.Year.ToString() + mm + dd;

              //string sqltxtEmpNo = "select C_Date,L_UID,C_Time from tEnter where L_UID=" + txtEmpName.Text + "and C_Date between'" + dtfrom + "'and'" + dtto + "'";

              string sql= "SELECT     c_date as [التاريخ],  l_uid as [رقم الموظف], c_Time as [الوقت]  FROM         tEnter WHERE     L_UID = " + txtEmpNo.Text+ " and c_date between '" + dtfrom + "' and '" + dtto+ "'";

              string strconn = "Provider=SQLOLEDB;Data Source=.;user id=sa;Initial Catalog=unis";
              OleDbConnection cn = new OleDbConnection(strconn);
              OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
              DataSet ds = new System.Data.DataSet();
              da.Fill(ds, "ta");
              if (ds == null)
                  return;


              if (ds.Tables[0].Rows.Count > 0)
              {

                  for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                  {



                      dtEmplogs.Rows.Add();

                      dtEmplogs.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i][0];

                      dtEmplogs.Rows[i].Cells[0].Value = DateTime.ParseExact(ds.Tables[0].Rows[i][0], "yyyyMMdd", CultureInfo.InvariantCulture)("yyyy/MM/dd");
                      dtEmplogs.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i][1];
                      dtEmplogs.Rows[i].Cells[2].Value = txtEmpName.Text;
                      dtEmplogs.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i][2];
                      dtEmplogs.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i][2];



                  }

                  dtEmplogs.DefaultCellStyle.SelectionBackColor = Color.BurlyWood;
              }
      }


What I have tried:

C#
<pre>  for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtEmplogs.Rows.Add();

                        dtEmplogs.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i][0];

                        dtEmplogs.Rows[i].Cells[0].Value = DateTime.ParseExact(ds.Tables[0].Rows[i][0], "yyyyMMdd", CultureInfo.InvariantCulture)("yyyy/MM/dd");
                        dtEmplogs.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i][1];
                        dtEmplogs.Rows[i].Cells[2].Value = txtEmpName.Text;
                        dtEmplogs.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i][2];
                        dtEmplogs.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i][2];

                    

                    }
Posted
Updated 22-Aug-17 7:32am
v2

Make sure that you pass a string to DateTime.ParseExact().
You must specify the exact formatting string for DateTime.ParseExact. You need to use a format string that has letters in it that tell ParseExact where to read in the values from your string.
See examples here: [Dotnetperls]
 
Share this answer
 
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900