I'm having trouble inserting the value of a masked textbox field into a database via a button click event.
I have a form that looks like this:
So basically you enter the data into the fields, click the button and it should Insert into the database using this code:
private void button1_Click(object sender, EventArgs e)
{
// Open connection with the SQL Server, using connection string
SqlConnection con = new SqlConnection("Data Source=MY-PC\\SQLEXPRESS;Initial Catalog=EnlaceDigital;User ID=user;Password=pass");
con.Open();
SqlCommand query = new SqlCommand("INSERT into Contratos VALUES ('" + this.teléfonoMaskedTextBox.Text + "', " +
"'" + this.folio_PISATextBox.Text + "', '" + this.tipo_de_TareaTextBox.Text + "', " +
"'" + this.estatusTextBox.Text + "','" + this.motivo_EstatusTextBox.Text + "','" + this.dTOTextBox.Text + "'," +
"'" + this.cOPETextBox.Text + "','" + this.no__ExpedienteTextBox.Text + "'," +
"'" + DateTime.Parse(this.fecha_InicialDateTimePicker.Text) + "', '" + DateTime.Parse(this.fecha_SolicitudDateTimePicker.Text) + "');", con);
query.ExecuteNonQuery();
MessageBox.Show("Added");
con.Close();
this.Close();
}
However, this method grabs the .Text property of the fields, so the Masked Textbox (field "Teléfono") writes the whole mask format into my database!
So in the database, I end up with something like "(012) 345 - 6789" instead of a 10 digit number.
I think this is happening because I'm basically inserting the content or Text of the textbox "as is", since I'm using this.teléfonoMaskedTextBox.Text
, and I don't know how to get the actual Value of the field, and not just the Text.
If it helps, the field "Teléfono" is of type nvarchar(50), so it "accepts" the mask format, but i need to insert into the database just the 10 digit phone number (without the mask), for other purposes.
Thank you.