I have a string that I need to convert to int before updating it in my database, but haven't managed to figure out how it's done. Here's my latest try:

int iLager = Int32.Parse(TextBoxProdNumberOf.Text);

        SqlConnection conn = new SqlConnection(config);
        conn.Open();
        string sql = "UPDATE Produkter SET iLager ='" + iLager + "' WHERE ID ='" + ID + "'";
        SqlCommand comm = new SqlCommand(sql, conn);
        comm.ExecuteNonQuery();//Conversion failed when converting the varchar value '__Page' to data type int.

I've also tried:
string iLager = Convert.ToInt32(TextBoxProdNumberOf.Text);
string iLager = int.Parse(TextBoxProdNumberOf.Text);

Any ideas? Thanks in advance!

The error wasn't in the conversion, but in the sql statement. This is the solution:

string BildID = TextBoxPicID.Text;
int iLager = Int32.Parse(TextBoxProdNumberOf.Text);
 
        SqlConnection conn = new SqlConnection(config);
        conn.Open();
        string sql = "UPDATE Produkter SET iLager ='" + iLager + "' WHERE ID ='" + BildID + "'";
        SqlCommand comm = new SqlCommand(sql, conn);
        comm.ExecuteNonQuery();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.