Vladson
Vladson

Reputation: 47

INSERT INTO MySQL through C#

I am making a WPF C# exc. I have a DAO class, with connection to my Database and also Service class, with some methods for getting information from Database. It works fine. But i want to insert to database also. So, where is my mistake? I have method in Service class with that code

public static DataTable createProject(string projectName, string depName, string empName, int estTime, DateTime startDate)
{
   string sql = "";
   sql += "INSERT INTO Projects (projectName, departmentName, employeeName, estimatedTime, startDate)";
   sql += "VALUES (" + projectName + depName + empName + estTime + startDate +")";

   return getDataTable(sql);
}

And after that, i am going to my xaml.cs

private void btnCreateAdd_Click(object sender, RoutedEventArgs e)
{
   Service.createProject((string)txtProjName.Text, (string)cmbCreateDepartment.SelectedItem, (string)cmbCreateEmployees.SelectedItem, Int32.Parse(txtElapseTime.Text), (DateTime)Calendar.SelectedDate);
}

It gives me some exeption in my xaml.cs

Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'.

Upvotes: 0

Views: 120

Answers (2)

NullReferenceException
NullReferenceException

Reputation: 1639

Try this

private void btnCreateAdd_Click(object sender, RoutedEventArgs e)
{
Service.createProject((string)txtProjName.Text, ((ComboBoxItem)cmbCreateDepartment.SelectedItem).Content.ToString(), ((ComboBoxItem)cmbCreateEmployees.SelectedItem).Content.ToString(), Int32.Parse(txtElapseTime.Text), (DateTime)Calendar.SelectedDate);
}

Upvotes: 0

NTinkicht
NTinkicht

Reputation: 1002

Look at the type of cmbCreateDepartment.SelectedItem and cmbCreateEmployees.SelectedItem attributes. It's a System.Data.DataRowView and not a String ! so the exception is logic.

Upvotes: 1

Related Questions