PK Singh

PK Singh

  • NA
  • 165
  • 14.3k

System.Data.SqlClient.SqlException: 'Conversion failed when converting

Mar 8 2021 7:39 AM
 i applied many conversion filled but not work please solved it in C#
 
System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value 'Ram' to data type int.'
  1. private void btnSave_Click(object sender, EventArgs e)  
  2. {  
  3. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);  
  4. // con.Open();  
  5. int value;  
  6. // foreach (CheckedListBox item in checkedListBox1.Items)  
  7. foreach (DataGridViewRow row in dgvFeeHeadName.Rows)  
  8. {  
  9. for (int i = 0; i < dgvFeeHeadName.Rows.Count; i++)  
  10. using (SqlCommand cmd = new SqlCommand("INSERT INTO mstFeePlan(ClassID,FeeHeadID,newamount,oldamount) VALUES(@Class,@Name, @Country,@Countrys)", con))  
  11. {  
  12. ///cmd.Parameters.AddWithValue("@CustomerId", row.Cells["Id"].Value);  
  13. cmd.Parameters.Clear();  
  14. //cmd.Parameters.AddWithValue("@Class", Convert.ToInt32( checkedListBox1.Text));  
  15. foreach (object item in checkedListBox1.CheckedItems)  
  16. {  
  17. DataRowView rowe = item as DataRowView;  
  18. cmd.Parameters.Clear();  
  19. cmd.Parameters.AddWithValue("@Class", rowe["ClassID"]);  
  20. cmd.Parameters.AddWithValue("@Name", row.Cells["Fee Head"].Value);  
  21. //cmd.Parameters.AddWithValue("@Name", (int)(row.Cells["Fee Head"].Value ?? DBNull.Value));  
  22. //cmd.Parameters.AddWithValue("@Name", row.Cells["Fee Head"].Value=Convert.ToInt32(dgvFeeHeadName.Columns["Fee Head"]));  
  23. //cmd.Parameters.Add("@Name", SqlDbType.Int).Value = dgvFeeHeadName.Rows[i].Cells["Fee Head"].Value;  
  24. cmd.Parameters.AddWithValue("@Country", Convert.ToInt32(row.Cells["New Fees"].Value));  
  25. cmd.Parameters.AddWithValue("@Countrys", Convert.ToInt32(row.Cells["Old Fees"].Value));  
  26. con.Open();  
  27. cmd.ExecuteNonQuery();  
  28. cmd.Parameters.Clear();  
  29. // dgvFeeHeadName.Rows.Clear();  
  30. //dgvFeeHeadName.Rows.Clear();  
  31. con.Close();  
  32. }  
  33. }  

Answers (2)