barmaglot
barmaglot

Reputation: 3

Error when converting date and/or time from character string

When I try to run this code, I'm receiving this error:

conversion failed when converting date and/or time from character string

I get the error on this line:

g_muallak_tarihi = DateTime.Parse(row_muallakt["Muallak_tarihi"].ToString());

To store values that are both dates and times or only dates, I use the DateTime data type. I'm not sure what to do. Do you have any ideas?

Here is my code:

public void cs_hmuallak()
{
    string cs_hmuallak = null;
    SqlConnection cnn;

    cs_hmuallak = @"Data Source= .\SQLEXPRESS; Initial Catalog=mydb;Integrated Security= True";

    cnn = new SqlConnection(cs_hmuallak);

    try
    {
        cnn.Open();

        {
                SqlDataAdapter da_muallakt = new SqlDataAdapter("SELECT M.Muallak_tutari FROM Muallak M INNER JOIN Hasar H ON M.Hasar_no= H.Hasar_no INNER JOIN Police P ON p.Police_no=h.Police_no where p.Acenta_kodu='"+cbx_acenta.Text+"' and p.Brans_kodu='"+cbx_brans.Text+"' and  M.muallak_tarihi<=(Select M.muallak_tarihi from muallak M where M.muallak_tarihi=(select max(muallak_tarihi) from muallak where muallak_tarihi<=@parameter)) and M.Muallak_tarihi=(SELECT max(M.Muallak_tarihi) FROM Muallak M INNER JOIN Hasar H ON M.Hasar_no= H.Hasar_no INNER JOIN Police P ON p.Police_no=h.Police_no where p.Acenta_kodu='"+cbx_acenta.Text+"' and p.Brans_kodu='"+cbx_brans.Text+"' and  M.muallak_tarihi<=(Select M.muallak_tarihi from muallak M where M.muallak_tarihi=(select max(muallak_tarihi) from muallak where muallak_tarihi<=@parameter)) ) ", cnn);

                da_muallakt.SelectCommand.Parameters.AddWithValue("@parameter",g_listetarihi.ToString("yyyy-mm-dd"));
                DataTable table_muallakt = new DataTable();
                da_muallakt.Fill(table_muallakt);
                foreach (DataRow row_muallakt in table_muallakt.Rows)
                {
                    if (row_muallakt["Muallak_tutari"] != DBNull.Value)
                    {
                        if (row_muallakt["Muallak_tarihi"] != DBNull.Value)
                        {
                            g_muallak_tarihi = DateTime.Parse(row_muallakt["Muallak_tarihi"].ToString());
                            g_muallak_tutari = int.Parse(row_muallakt["Muallak_tutari"].ToString());
                        }
                    }
                }                   
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}       

Upvotes: 0

Views: 711

Answers (2)

Umesh
Umesh

Reputation: 2742

Use string.Format

da_muallakt.SelectCommand.Parameters.AddWithValue("@parameter", string.Format("{0:yyyy-MM-dd}",g_listetarihi.ToString());

MSDN DateTime formatting: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Upvotes: -1

Oded
Oded

Reputation: 499272

Assuming that Muallak_tarihi is defined as a DATETIME column (which is should be as that's what it is holding), all this work with converting to and from strings is not needed.

You can simply use GetDateTime directly with the data reader in order to get it, or simply cast the value:

g_muallak_tarihi = row_muallakt["Muallak_tarihi"] as DateTime;

Additionally, the parameter you are using (@parameter) should also be a DATETIME - and simply passed in directly, instead of using ToString:

da_muallakt.SelectCommand.Parameters.AddWithValue("@parameter",g_listetarihi);

Upvotes: 2

Related Questions