Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format object ToString

How do I format the two strings Data and Somma in this context?

 SqlConnection con = new SqlConnection(strConnString);
    con.Open();
    str = "select * from Pagamenti ORDER BY [Data] DESC";
    com = new SqlCommand(str, con);
    sqlda = new SqlDataAdapter(com);
    ds = new DataSet();
    sqlda.Fill(ds, "Pagamenti");

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        DropDownList1.Items.Add(ds.Tables[0].Rows[i]["Id"] +
            "   --|--   " + ds.Tables[0].Rows[i]["Data"].ToString() +
            "   --|--   " + ds.Tables[0].Rows[i]["Somma"]);
    }
    con.Close();

ToString() is not taking anything and I need respectively "dd/MM/yyyy" and "R #.###".

like image 207
FeliceM Avatar asked Jan 22 '14 12:01

FeliceM


2 Answers

I guess that is because the type of the values returned is object, which indeed has not parameters.

Try to cast the object to the right type and call ToString again.

Like this:

Convert.ToDateTime(ds.Tables[0].Rows[i]["Data"]).ToString("dd/MM/yyyy")

Or let the string.Format handle it:

string.Format("{0:dd/MM/yyyy}", ds.Tables[0].Rows[i]["Data"])
like image 75
Patrick Hofman Avatar answered Sep 21 '22 21:09

Patrick Hofman


string.Format("{0} --|-- {1} --|-- {2}",ds.Tables[0].Rows[i]["Id"].ToString(),ds.Tables[0].Rows[i]["Data"].ToString(),ds.Tables[0].Rows[i]["Somma"].ToString());
like image 24
NileshChauhan Avatar answered Sep 21 '22 21:09

NileshChauhan