Question

What's wrong with my code??

  string birthdate = ((DataRowView)comboBox1.SelectedItem).Row["birthdate"].ToString(); //pulled the data into the database ()

string[] split = birthdate.Split('/'); //split the Date

I want to put them in a textbox so I thought of doing this:

textbox1.Text = split[0]; //correct, gets the 1st word the (Day)
    textbox2.Text = split[1]; //incorrect, outofrange exception (Month)
    textbox3.Text = split[2]; //incorrect, outforange exception (Year)

NOTE: The format is Day/Month(words)/Year ==> 1/January/2012

Can somebody help me get that values and put them one by one in a textbox?

Was it helpful?

Solution

This problem really got started by storing dates in a column of type varchar. It takes only one machine with the culture set wrong to damage the database table so all machines that try to read it will bomb. Solve the real problem, fix the table.

Anyhoo, you'll want to improve your code so the dbase admin will have a fighting chance to repair the damage. Throw an exception that gives sufficient information. Something like:

string[] split = birthdate.Split('/');
if (split.Length != 3) {
    throw new Exception("Invalid date string for table entry " + row["primarykey"].ToString());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top