assign value to datetimepicker (windows form)?

i have a datetimepicker in a window form.

how should the coding be if i read a date from the database and the datatimepicker will show the date that i get from database

something like if i read a data from database and assign the data to the textbox, i would write

SqlDataReader myReader;
myReader = cmd3.ExecuteReader();
if (myReader.Read())
{
txtunit.Text=myReader["P_UNITS"].ToString(); //textbox

}

wat is the coding if i now want to assign the value from database to a datetimepicker



Answer this question

assign value to datetimepicker (windows form)?

  • Ego_Opinion

    try,

    int index = combobox.FindString(myReader["ColumnName"]);

    combobox.SelectedIndex = index;

    You can also use the combobox.FindStringExact method if you want an exact match on the two values

    hope this helps


  • chinmaykshah

    You asked the question how to add a datetime value to a combobox. But i don't see the relationship between a datatime object and buy/sell

    When do you want to select buy and when sell

  • EmadH

    how about if it is a combobox what should i write


  • bigkdogg

    i also dont know why i using this code, it wont show.....

    but if u replace the status with exactly the word "BUY", it's work.....i very sure there is data in my database, but why

    String status = (String)myReader["ColumnBuySell"]; //not work...
    int index = combobox.FindString( status );

    int index = combobox.FindString("BUY"); //THIS ONE CAN WORK...WHY


  • stu5601

    Do you get an exception, pleace provide us more details about why it don't work!


  • Dcopus

    sorry everyone....

    the coding is correct.....it's my data error.....so the problem solved....

    thank you....


  • Shravan Kumar G

    I'm sorry that i confused you...actually tat is another question, it does not have relation with the datetime and with question 1...

    the question is like this....

    i have a combobox in my window form which i set the item collection to buy and sell...

    then now i read the data from database, the data is either buy and sell also, how can i assign the data to the combobox.....for example the data now is buy, how can the combobox show buy now

    hope you can understand


  • marianf


    String status = (String)myReader["ColumnBuySell"];
    int index = combobox.FindString( status );

    if( index == -1 )
    {
    index = combobox.Items.Add( status );
    }

    combobox.SelectedIndex = index;




  • UWO

    But have you looked at the value of the status string variable with your debugger Have you debugged the code


  • Dusan Tkac

    thank you for keep replying me....

    but i still have a doubt  for the second question i asked...

    Actually i have a combobox which item collection i have add in two item(buy , sell)...

    then when i read the data(either buy or sell) from database, the combobox will find the text and show it.....how the code is for this situation Because it used the above code, it cant work....it cant find the index of the item in the combobox....it will execute the if else statment, ....but the problem is i dont want to add another word "buy" into the combobox again.......i just want to display it....

    thank you.....


  • Dhart

    "not work" in the previous post doesnt mean error....

    just the combobox doesnt select the word and show it .....

    but if i put "BUY" directly , it works, the combobox will select the word from the item collection and show it....

    dont know why this situation happen....wondering...


  • Nikhil27

    The DateTimePicker control class has a Value propery. You can set this to show a specified time on the control.


    myDateTimePicker.Value = (DateTime)myReader["P_UNITS"];



  • codeblaster


    DateTime date = (DateTime)reader["MyDataTimeField"];

    int index = combobox.Items.IndexOf( date );

    if( index == -1 )
    {
    index = combobox.Items.Add( date );
    }

    combobox.SelectedIndex = index;




  • Samiha Esha

    Did you make sure to replace "ColumnBuySell" with the column name in your database
  • assign value to datetimepicker (windows form)?