7
Answers

Its Visual Studio 2013. How do I convert string to DateTime?

A A

A A

10y
2.2k
1
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);
  }
  }
}


Answers (7)