Updating Database on button click

i have a button that suppose to update data into the database.

private void button4_Click(object sender, EventArgs e)
        {
            //need update code//
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
            conn.Open();

            SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = @cName", conn);
            daCount.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;

            DataTable dtC = new DataTable();
            daCount.Fill(dtC);
            DataRow firstRow = dtC.Rows[0];

            string x = firstRow["iCount"].ToString();
            int y = Int32.Parse(x);
            int z = y + 1;

            //SqlCeCommand cmdC = conn.CreateCommand();
            SqlCommand cmdC = conn.CreateCommand();
            cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
                conn.Close();
}

but i get this error..

the error

can someone help?

update =

i've changed my code to

cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";

but the problem now is that , there's no update.

the iCount in the database is an INT , value is 0. There is also no update for the viewtime and lastview.

where did i go wrong now?

Upvotes: 2

Views: 3956

Answers (3)

DROP TABLE users
DROP TABLE users

Reputation: 1955

I would guess maybe the icount value is not a number, i would recommend using TryParse just in case. And that should keep this error from happening. What to do about a bad value getting returned by the query is another issue.

private void button4_Click(object sender, EventArgs e)
        {
            //need update code//
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
            conn.Open();

            SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = @cName", conn);
            daCount.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;

            DataTable dtC = new DataTable();
            daCount.Fill(dtC);
            DataRow firstRow = dtC.Rows[0];

            string x = firstRow["iCount"].ToString();

            int y = 0;
            if(Int32.TryParse(x,out y))
            {      
                System.Diagnostics.Debug.WriteLine("iCount was an valid int32");      
                int z = y + 1;

                //SqlCeCommand cmdC = conn.CreateCommand();
                SqlCommand cmdC = conn.CreateCommand();
                cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
             }
            else
                System.Diagnostics.Debug.WriteLine("iCount was NOT a valid int32, value: " + x);
             conn.Close();
}

Upvotes: 1

logixologist
logixologist

Reputation: 3834

change this:

    cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";

to

    cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";

you dont need the "'" apostrophe around it becuase its a number. That would definitely get you string not in correct format error

Upvotes: 2

Paulo Correia
Paulo Correia

Reputation: 616

Have you checked the value of the 'x' variable? The exception informs that the value of X isn't a valid integer, so the FormatException is thrown.

Upvotes: 0

Related Questions