3

I have got this code below which care whether maskedtextbox is empty. If it is empty it INSERT INTO SQL table __.__.____ - only the mask so I believe it INSERTS NULL But something got wrong when I fill the maskedtextbox. It still INSERT __.__.____ - NULL I don't know where I make mistake because it should normally insert the date which is in the maskedtextbox.

The mask for this textbox is __.__.____ like 00/00/0000

Data type in which I insert this data is - date.

Here is the code:

var value2 = (object)DBNull.Value;
DateTime parsedDate2;
if (DateTime.TryParseExact(maskedTextBox2.Text, "__.__.____", null,
                           DateTimeStyles.None, out parsedDate2))
{
    value2 = parsedDate2;
}
prikaz.Parameters.AddWithValue("@odjdate", value2);

Edit : This code below says: the name 'value2' does not exist in the current context'

var value2 = (object)DBNull.Value;
DateTime parsedDate2;
if (DateTime.TryParseExact(maskedTextBox2.Text, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate2))
{
    value2 = parsedDate2;
}
prikaz.Parameters.AddWithValue("@odjdate", value2);

This code below does this Ex:

Converting into data type date wasn't succesfull.

This is the code

if (maskedTextBox2.Text == "__.__.____")
{
    prikaz.Parameters.AddWithValue("@odjdate", null);
}
else
{
    prikaz.Parameters.AddWithValue("@odjdate",maskedTextBox2.Text);
}
chue x
  • 18,573
  • 7
  • 56
  • 70
Marek
  • 3,555
  • 17
  • 74
  • 123
  • 1
    You have to give the format in `DateTime.TryParseExact` instead of `__...` what is your mask format? – Sriram Sakthivel Aug 05 '13 at 14:15
  • @SriramSakthivel Thank you for your comment, my mask is 00/00/0000 - / is replaced with . – Marek Aug 05 '13 at 14:16
  • 1
    How does your `maskedTextBox2.Text` value looks like? show sample – Sriram Sakthivel Aug 05 '13 at 14:20
  • 12.12.2012 This is example – Marek Aug 05 '13 at 14:20
  • 1
    MM.dd.yyyy or dd.MM.yyyy ? – Sriram Sakthivel Aug 05 '13 at 14:24
  • Im not sure :( In SQL database it is like 2012-12-12, but I insert it like 12.12.2012 - dd.MM.yyyy atleast I hope first is day then is month and year is in the end. – Marek Aug 05 '13 at 14:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34849/discussion-between-marek-and-sriram-sakthivel) – Marek Aug 05 '13 at 15:06
  • @Marek by default, your `maskedTextBox.Text` will be like as what you can see but if you modify the `MaskedTextBox.TextMaskFormat` such as `maskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;`, it will be different, for example, `12.12.2012` will be `12122012` and it could be your problem. – King King Aug 05 '13 at 22:13

2 Answers2

1

Try this

var value2 = (object)DBNull.Value;
DateTime parsedDate2;
if (DateTime.TryParseExact(maskedTextBox2.Text, "dd.MM.yyyy", CultureInfo.InvariantCulture,DateTimeStyles.None, out parsedDate2))
{
    value2 = parsedDate2;
}

Note: if your format is "MM.dd.yyyy" then replace format to make the parse to work!

This should work

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

This works fine for me:

 var value2 = (Object)DBNull.Value;
 DateTime parsedDate2;
 if (DateTime.TryParse(maskedTextBox1 .Text, out parsedDate2))
 {
   value2 = (Object)parsedDate2;
 }
prikaz.Parameters.AddWithValue("@odjdate", value2);     
Silvano
  • 203
  • 1
  • 7