0

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'.

Mighty Badaboom
6,1735 gold badges37 silver badges54 bronze badges
asked Mar 20, 2014 at 9:57
3
  • 1
    possible duplicate stackoverflow.com/questions/20346481/… Commented Mar 20, 2014 at 10:04
  • In which line you are getting the exception, put a breakpoint and check Commented Mar 20, 2014 at 10:04
  • at Service.createProject() line Commented Mar 20, 2014 at 10:06

2 Answers 2

1

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.

answered Mar 20, 2014 at 10:14
Sign up to request clarification or add additional context in comments.

Comments

0

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);
}
answered Mar 20, 2014 at 10:38

5 Comments

Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Controls.ComboBoxItem'. This Exception
@Vladson cmbCreateDepartment i hope this is ComboBox name.?
@Vladson Then there is binding mistake.please go through this stackoverflow.com/questions/6420300/…
Now i have this error : There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement. But i have the right number of columns
try using this sql = "INSERT INTO Projects VALUES('" + projectName +"','"+ depName +"','"+empName +"','"+ estTime +"','"+startDate +"')";

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.