cannot convert value of parameter date from system string to system datetime

后端 未结 5 1168
终归单人心
终归单人心 2020-12-22 15:31
public Kupac(SqlDataReader reader)
{
    KupacId = Convert.ToInt32(reader[\"KupacId\"]);
    Ime = reader[\"Ime\"].ToString();
    Prezime = reader[\"Prezime\"].ToSt         


        
相关标签:
5条回答
  • 2020-12-22 15:59

    This is all from my class KupacAdapter and i thin errror is here somewhere

    public class KupacAdapter {

    public KupacAdapter()
    {
    
    }
    
    public static List<Kupac> VratiKupce(int kriterijumPretrage, string tekstPretrage)
    {
        List<Kupac> listaKupaca = new List<Kupac>();
    
        SqlConnection konekcija = new SqlConnection();
    
        try
        {
            konekcija.ConnectionString = CONNECTION_STRING;
            konekcija.Open();
    
            SqlCommand komanda = new SqlCommand();
            komanda.Connection = konekcija;
    
            string selectUpit = "select * from Kupac where 1=1 ";
    
            if (!string.IsNullOrEmpty(tekstPretrage))
            {
                switch (kriterijumPretrage)
                {
                    case 0:
                        selectUpit += " and IdentifikacioniBroj LIKE '%' + @kriterijum + '%'";
                        break;
                    case 1:
                        selectUpit += " and Ime LIKE '%' + @kriterijum + '%'";
                        break;
                    case 2:
                        selectUpit += " and Prezime LIKE '%' + @kriterijum + '%'";
                        break;
                }
    
                komanda.Parameters.AddWithValue("@kriterijum", tekstPretrage);
            }
            komanda.CommandText = selectUpit;
    
    
            SqlDataReader reader = komanda.ExecuteReader();
    
            while (reader.Read())
            {
                listaKupaca.Add(new Kupac(reader));
            }
            reader.Close();
    
            return listaKupaca;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            konekcija.Close();
        }
    }
    
    public static void InsertKupac(Kupac kupac)
    {
        SqlConnection konekcija = new SqlConnection();
        try
        {
            konekcija.ConnectionString = CONNECTION_STRING;
            konekcija.Open();
    
            string insertUpit = "INSERT INTO Kupac(Ime, Prezime, IdentifikacioniBroj, ClanOd, KorisnickoIme) "
                                + "VALUES(@Ime, @Prezime, @IdentifikacioniBroj, GETDATE(), @KorisnickoIme)";
    
            SqlCommand komanda = new SqlCommand(insertUpit, konekcija);
            komanda.Parameters.AddWithValue("@Ime", kupac.Ime);
            komanda.Parameters.AddWithValue("@Prezime", kupac.Prezime);
            komanda.Parameters.AddWithValue("@IdentifikacioniBroj", kupac.IdentifikacioniBroj);
            komanda.Parameters.AddWithValue("@KorisnickoIme", kupac.KorisnickoIme);
    
            komanda.ExecuteNonQuery();
        }
        catch
        {
    
        }
        finally
        {
            konekcija.Close();
        }
    }
    
    public static void UpdateKupac(Kupac kupac)
    {
        SqlConnection konekcija = new SqlConnection();
        try
        {
            konekcija.ConnectionString = CONNECTION_STRING;
            konekcija.Open();
    
            string updateUpit = @" UPDATE [Kupac] 
                                   SET [Ime] = @Ime, [Prezime] = @Prezime, 
                        [IdentifikacioniBroj] = @IdentifikacioniBroj
                                   WHERE [KupacId] = @KupacId";
    
            SqlCommand komanda = new SqlCommand(updateUpit, konekcija);
            komanda.Parameters.Add("@Ime", kupac.Ime);
            komanda.Parameters.Add("@Prezime", kupac.Prezime);
            komanda.Parameters.Add("@IdentifikacioniBroj", kupac.IdentifikacioniBroj);
            komanda.Parameters.Add("@KupacId", kupac.KupacId);
    
            komanda.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            konekcija.Close();
        }
    }
    
    public static void DeleteKupac(Kupac kupac)
    {
        SqlConnection konekcija = new SqlConnection();
        try
        {
            konekcija.ConnectionString = CONNECTION_STRING;
            konekcija.Open();
    
            string deleteUpit = @" DELETE 
                                   FROM Kupac
                                   WHERE KupacId = @KupacId";
    
            SqlCommand komanda = new SqlCommand(deleteUpit, konekcija);
            komanda.Parameters.Add("@KupacId", kupac.KupacId);
    
            komanda.ExecuteNonQuery();
        }
        catch
        {
    
        }
        finally
        {
            konekcija.Close();
        }
    }
    
    public static DataTable VratiSveKupce()
    {
        DataTable dtSviKupci = new DataTable();
    
        SqlConnection konekcija = new SqlConnection();
    
        try
        {
            konekcija.ConnectionString = CONNECTION_STRING;
            konekcija.Open();
    
            string selectUpit = @"SELECT        KupacId, Ime, Prezime, IdentifikacioniBroj, ClanOd,
                                Ime + ' ' + Prezime + ' - ' + IdentifikacioniBroj AS PunoIme
    
                                FROM            Kupac
                                Order by Ime, Prezime, IdentifikacioniBroj";
    
            SqlDataAdapter da = new SqlDataAdapter(selectUpit, konekcija);
            da.Fill(dtSviKupci);
        }
        catch
        {
            dtSviKupci = null;
        }
        finally
        {
            konekcija.Close();
        }
        return dtSviKupci;
    }
    
    public static int VratiKupacIdZaKorisnickoIme(string korisnickoIme)
    {
        SqlConnection konekcija = new SqlConnection();
    
        try
        {
            konekcija.ConnectionString = CONNECTION_STRING;
            konekcija.Open();
    
            SqlCommand komanda = new SqlCommand();
            komanda.Connection = konekcija;
    
            string selectUpit = "select * from Kupac where KorisnickoIme=@KorisnickoIme";
            komanda.Parameters.AddWithValue("@KorisnickoIme", korisnickoIme);
            komanda.CommandText = selectUpit;
    
            return Convert.ToInt32(komanda.ExecuteScalar());
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            konekcija.Close();
        }
    }
    

    }

    0 讨论(0)
  • 2020-12-22 16:05

    You might need to specify culture

    ClanOd = DateTime.ParseExact(reader["ClanOd"], "dd.MM.yyyy", System.Globalization.CultureInfo.GetCultureInfo("de-DE"));
    

    Or try this:

    System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("de-DE");
    ClanOd = DateTime.Parse(reader["ClanOd"], cultureInfo);
    
    0 讨论(0)
  • 2020-12-22 16:06

    public Kupac( SqlDataReader reader) { try { KupacId = Convert.ToInt32(reader["KupacId"]); Ime = reader["Ime"].ToString(); Prezime = reader["Prezime"].ToString(); IdentifikacioniBroj = reader["IdentifikacioniBroj"].ToString(); ClanOd = (DateTime)reader["ClanOd"]; KorisnickoIme = reader["KorisnickoIme"].ToString(); } catch (Exception err) { throw err; } }

    //public Kupac(SqlDataReader reader)
    //{
    //    KupacId = Convert.ToInt32(reader["KupacId"]);
    //    Ime = reader["Ime"].ToString();
    //    Prezime = reader["Prezime"].ToString();
    //    IdentifikacioniBroj = reader["IdentifikacioniBroj"].ToString();
    //    ClanOd = (DateTime) reader["ClanOd"];        
    //    KorisnickoIme = reader["KorisnickoIme"].ToString();
    //}
    
    public int KupacId
    {
        get;
        set;
    }
    
    public string Ime
    {
        get;
        set;
    }
    
    public string Prezime
    {
        get;
        set;
    }
    
    public string IdentifikacioniBroj
    {
        get;
        set;
    }
    
    public DateTime ClanOd
    {
        get;
        set;
    }
    
    public string KorisnickoIme
    {
        get;
        set;
    
    }
    

    }

    0 讨论(0)
  • 2020-12-22 16:22

    You can't cast a string into a date time but you can use DateTime's Parse:

    ClanOd = DateTime.Parse(reader["ClanOd"]);
    
    0 讨论(0)
  • 2020-12-22 16:22

    what Smith.h.Neil said, but I would use the other version of try parse if the value could be an invalid date.

     string validDate = "2014-04-17";
     string invalidDate = "not a date";
     DateTime date;
     DateTime date2;
     bool isValidDate = DateTime.TryParse(validDate, out date);
     bool isValidDate2 = DateTime.TryParse(invalidDate , out date2);
    

    http://msdn.microsoft.com/en-us/library/system.datetime.aspx

    Assuming ClanOd is a DateTime Replace

    ClanOd = (DateTime)reader["ClanOd"];
    

    With

    DateTime date;
    bool isValid = DateTime.TryParse(reader["ClanOd"].ToString(), out date);
    if(isValid)
        ClanOd  = date;
    
    0 讨论(0)
提交回复
热议问题