I have created a database and a windows form application in C# on Visual Studio 2013. I get an error "Cannot convert datetimepicker from character string" How can I convert the birthdateDateTimePicker from character string in my code? do I have to use a cast operater?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Practice_3
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void patientBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.patientBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.medicaldblDataSet);
}
private void MainForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'medicaldblDataSet.Patient' table. You can move, or remove it, as needed.
this.patientTableAdapter.Fill(this.medicaldblDataSet.Patient);
}
private void btnSave_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(global::Practice_3.Properties.Settings.Default.MedicaldblConnectionString);
try
{
string sql = "Insert into Patient (PatientID, PatientName, PatientLastName, Birthdate, Alias) Values (" + patientIDTextBox.Text + ",'"+ patientNameTextBox.Text + "','" + patientLastNameTextBox.Text + "','" + birthdateDateTimePicker + "','" + aliasTextBox.Text + "')";
SqlCommand pro = new SqlCommand(sql, conn);
conn.Open(); //open the database
pro.ExecuteNonQuery();
MessageBox.Show("The data was saved successfully","", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.patientTableAdapter.Fill(this.medicaldblDataSet.Patient);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
this.patientTableAdapter.Fill(this.medicaldblDataSet.Patient);
}
}
}
Loading
A APosted Oct 31, 2014, 8:18 PM
1.) The first error is when I am filling out the fields i use the TAB key to move from one textbox to the next but when I hit the TAB key to go to the last textbox all of my data diappears from all the fields and a -1 appears in my DataGridView (and non of the data is saved in any of the columns)
2.)My second error is when I hit the save button an error pops up and says:
INCORRECT SYNTAX NEAR ".
I have looked for information but i cannot find any real solution to these problems.
here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
namespace database_project
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void tblPatientBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.tblPatientBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this._DoctorRecords_SQLServer_DataSet);
}
private void MainForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the '_DoctorRecords_SQLServer_DataSet.tblPatient' table. You can move, or remove it, as needed.
this.tblPatientTableAdapter.Fill(this._DoctorRecords_SQLServer_DataSet.tblPatient);
}
private void btnSave_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=MainUser;Initial Catalog=DoctorRecords(SQLServer);Integrated Security=True");
try
{
string say = ("insert into tblPatient(PatientID, Name, LastName, RAddress, PAddress, " +
" Alias, HomeTel, Cell, Email, Birthdate, [Where do you spend most of your time?], [Friends and Family], " +
"[Homeless or Sheltered], [Where do you sleep]) Values('" +
patientIDTextBox.Text + "','" + nameTextBox.Text + "','" + lastNameTextBox.Text + "','" + rAddressTextBox.Text +
"','" + pAddressTextBox.Text + "','" + aliasTextBox.Text + "','" + homeTelTextBox.Text + "','" + cellTextBox.Text + "','" + emailTextBox.Text +
"','" + timeSpentTextBox.Text + "','" + fFamilyTextBox.Text + "','" + homeless_ShelteredTextBox.Text + "','" + sleepTextBox.Text + "'");
SqlCommand yay = new SqlCommand(say, conn);
conn.Open();
yay.ExecuteNonQuery();
MessageBox.Show("Data successfully saved", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
patientIDTextBox.Clear();
nameTextBox.Clear();
lastNameTextBox.Clear();
rAddressTextBox.Clear();
pAddressTextBox.Clear();
aliasTextBox.Clear();
homeTelTextBox.Clear();
cellTextBox.Clear();
emailTextBox.Clear();
timeSpentTextBox.Clear();
fFamilyTextBox.Clear();
homeless_ShelteredTextBox.Clear();
sleepTextBox.Clear();
conn.Close();
}
}
}
}
VulpesPosted Oct 30, 2014, 10:59 AM
A APosted Oct 29, 2014, 10:53 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Practice
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the '_DoctorRecords_SQLServer_DataSet.tblPatient' table. You can move, or remove it, as needed.
this.tblPatientTableAdapter.Fill(this._DoctorRecords_SQLServer_DataSet.tblPatient);
SqlConnection conn = new SqlConnection("Data Source=MainUser;Initial Catalog=DoctorRecords(SQLServer);Integrated Security=True");
try
{
string say = ("Insert into tblPatient(PatientID, Name, LastName, RAddress, PAddress, Birthdate, Alias, HomeTel, Cell, Email, DateAttended, Sex, [Where do you spend most of your time?], [Friends and Family], [Homeless or Sheltered], [Where do you sleep]) Values('" +
patientIDTextBox.Text + "','" + nameTextBox.Text + "','" + lastNameTextBox.Text + "','" + rAddressTextBox.Text +
"','" + pAddressTextBox.Text + "','" + birthdateDateTimePicker.Value.ToString("yyyy, MM, dd") + "','" + aliasTextBox.Text + "','" + homeTelTextBox.Text + "','" + cellTextBox.Text + "','" + emailTextBox.Text +
"','" + dateAttendedDateTimePicker.Value.ToString("yyyy, MM, dd") + "','" + sexCheckBox + "','" + timeSpentTextBox.Text + "','" + fFamilyTextBox.Text + "','" + homeless_ShelteredTextBox.Text + "','"
+ sleepTextBox.Text + "','" + yesPicCheckBox.ToString() + "','" + noPicCheckBox.ToString() + "'");
SqlCommand yay = new SqlCommand(say, conn);
conn.Open();
yay.ExecuteNonQuery();
if (sexCheckBox == null && femaleCheckBox == null)
{
MessageBox.Show("Please Enter your sex", "Data Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (yesPicCheckBox == null)
{
noPicCheckBox.Checked = true;
}
MessageBox.Show("Data saved successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
patientIDTextBox.Clear();
nameTextBox.Clear();
lastNameTextBox.Clear();
rAddressTextBox.Clear();
pAddressTextBox.Clear();
aliasTextBox.Clear();
homeTelTextBox.Clear();
cellTextBox.Clear();
emailTextBox.Clear();
timeSpentTextBox.Clear();
fFamilyTextBox.Clear();
homeless_ShelteredTextBox.Clear();
sleepTextBox.Clear();
conn.Close();
}
}
private void tblPatientBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.tblPatientBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this._DoctorRecords_SQLServer_DataSet);
}
}
}
VulpesPosted Oct 29, 2014, 11:01 AM
Are you sure that your connection string is OK and that in particular your userid and password are correct?
The only other (remote) possibility is that a trigger is replacing the insert command with something else.
A APosted Oct 29, 2014, 10:42 AM
VulpesPosted Oct 29, 2014, 7:50 AM
Sibeesh VenuPosted Oct 29, 2014, 7:21 AM
Find out more here : http://vb.net.bigresource.com/SQL-Exception-on-Insert-of-dateandtimepicker-value-date-into-date-datatype-Conversion-failed-when-converting-date-and-or-time-from-character-string--3MIjsqgCt.html