Click here to Skip to main content
15,920,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<big>  if(maskedTextBox1.Text == "")
                {
                   
                    maskedTextBox1.Text = "##/##/#####";

                }</big> //[i do it like that]
                    string sc = ConfigurationManager.ConnectionStrings["dcon2"].ConnectionString;
                    OdbcConnection con = new OdbcConnection();
                    con.ConnectionString = sc;
                    con.Open();
                    string query = "insert into fee_payments_mst(c_code,c_class,c_amount,c_cash,c_cheque,c_date,c_cheque_date,c_chequeno,c_bankname,c_branch_name,c_user) values(?,?,?,?,?,?,?,?,?,?,?)";
                    cmd = new OdbcCommand(query, con);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@C_code", s );
                    cmd.Parameters.AddWithValue("@c_class", comboBox2.Text);
                    cmd.Parameters.AddWithValue("@c_amount", textBox1.Text);
                    cmd.Parameters.AddWithValue("@c_cash", 1);
                    cmd.Parameters.AddWithValue("@c_cheque", 0);
                    cmd.Parameters.AddWithValue("@c_date",Convert .ToDateTime  ( maskedTextBox2.Text));



                    cmd.Parameters.AddWithValue("@c_cheque_date", Convert .ToDateTime (maskedTextBox1 .Text ));
                    cmd.Parameters.AddWithValue("@c_chequeno", textBox3.Text);
                    cmd.Parameters.AddWithValue("@c_bankname", comboBox4.Text);

                    cmd.Parameters.AddWithValue("@c_branchnaame", textBox2.Text);
                    cmd.Parameters.AddWithValue("@c_user", Txtuser.Text);
                    cmd.ExecuteNonQuery();
                    con.Close();


                    MessageBox.Show("the record has been inserted successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

i want to insert maskedtextbox1.text as null value to my sybase databse.but am not able to do that.i also try to insert that as like maskedtextbox.text="##/##/####";but a error was fired that the [the string is not a valid] ;
Posted
Updated 19-Jun-12 21:14pm
v2
Comments
StM0n 20-Jun-12 3:32am    
Which line throws the exception?
And have it to be a null or would an empty string be ok?

Why would you expect that to work?
If you set a string to "##/##/####" why would you expect that to convert nicely to a DateTime value?

In fact, why are you using a MaskedTextBox for this at all? Why not use a DateTimePicker instead? It has a Value property which is a DateTime to start with, and which is always a valid date. With your version, I could enter "72/49/7321" and you would try to insert that to your DB!
 
Share this answer
 
Comments
Priyam Dey Biswas 20-Jun-12 4:11am    
i want to tell u that through only the posted code am able to work,so please give some possible solution with that,how can i insert null value into my sybase database,through maskedtextbox in c#.
OriginalGriff 20-Jun-12 4:14am    
Then you can't use the convert function.
Instead use the DateTime.TryParse and if the convertion doesn't work then insert DBNull.Value to your database instead.
However, if you user enters 31 feb 2012 it will insert a null instead of telling him!
Seriously, use a DateTime picker instead!
As an extension to Griffs answer, you could also simply check, if the given Text could be parsed as an DateTime... the user is free to choose the prefered format.

If it isn't a date, you could save an empty string.

[edit after response]

C#
if(maskedTextBox1.Text == "") {
    maskedTextBox1.Text = "##/##/#####";
}

//[...]

cmd.Parameters.AddWithValue("@c_cheque_date", Convert.ToDateTime (maskedTextBox1 .Text ));

//[...]


Ok... this is where youre code breaks...

You should rather check upfront if there's a valid DateTime in the maskedTextBox1 and store it in a variable.

D'you need some code or could you handle it from here on?
 
Share this answer
 
v2
Comments
Priyam Dey Biswas 20-Jun-12 4:07am    
cmd.Parameters.AddWithValue("@c_cheque_date", Convert .ToDateTime (maskedTextBox1 .Text )); -->in this line the error was fired["the string was not recognized as a valid date time"].u can go thorough my whole code,which i posted previously,for to find the error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900