Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void btnSave_Click(object sender, EventArgs e)  
{  
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);  
// con.Open();  
int value;  
// foreach (CheckedListBox item in checkedListBox1.Items)  
foreach (DataGridViewRow row in dgvFeeHeadName.Rows)  
{  
for (int i = 0; i < dgvFeeHeadName.Rows.Count; i++)  
using (SqlCommand cmd = new SqlCommand("INSERT INTO mstFeePlan(ClassID,FeeHeadID,newamount,oldamount) VALUES(@Class,@Name, @Country,@Countrys)", con))  
{  
///cmd.Parameters.AddWithValue("@CustomerId", row.Cells["Id"].Value);  
cmd.Parameters.Clear();  
//cmd.Parameters.AddWithValue("@Class", Convert.ToInt32( checkedListBox1.Text));  
foreach (object item in checkedListBox1.CheckedItems)  
{  
DataRowView rowe = item as DataRowView;  
cmd.Parameters.Clear();  
cmd.Parameters.AddWithValue("@Class", rowe["ClassID"]);  
cmd.Parameters.AddWithValue("@Name", row.Cells["Fee Head"].Value);  
//cmd.Parameters.AddWithValue("@Name", (int)(row.Cells["Fee Head"].Value ?? DBNull.Value));  
//cmd.Parameters.AddWithValue("@Name", row.Cells["Fee Head"].Value=Convert.ToInt32(dgvFeeHeadName.Columns["Fee Head"]));  
//cmd.Parameters.Add("@Name", SqlDbType.Int).Value = dgvFeeHeadName.Rows[i].Cells["Fee Head"].Value;  
cmd.Parameters.AddWithValue("@Country", Convert.ToInt32(row.Cells["New Fees"].Value));  
cmd.Parameters.AddWithValue("@Countrys", Convert.ToInt32(row.Cells["Old Fees"].Value));  
con.Open();  
cmd.ExecuteNonQuery();  
cmd.Parameters.Clear();  
// dgvFeeHeadName.Rows.Clear();  
//dgvFeeHeadName.Rows.Clear();  
con.Close();  
}  
}  


What I have tried:

System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value 'Ram' to data type int
Posted
Updated 8-Mar-21 4:41am

1 solution

We can't help you: the error is very legitimately telling you that one of the values you are passing to SQL is the word "ram" and it expects a numeric value becuas ethat value is going into an INTEGER column.

And that is data dependant - we have no idea which value is "ram" or what column it might be going into.

So, it's going to be up to you.
ortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900