简体   繁体   中英

how update table from datagridview and textbox

i user this code to update data from datagridview to my sql server table bout i didn't get any error and the data didn't updated so where is the problem in my code.

the connection is correct too.

  using (SqlConnection con = new SqlConnection("**"))
        {
            con.Open();


            using (SqlCommand com = new SqlCommand("UPDATE indebtedness SET collected=@collected,Payment_Date=@Payment_Date WHERE Subscriber_No=@Subscriber_No and company_name=@company_name and indebtedness_name=@indebtedness_name", con))
            {
                com.Parameters.AddWithValue("@company_name", company_name.Text);
                com.Parameters.AddWithValue("@indebtedness_name", indebtedness_name.Text);
                com.Parameters.Add("@Payment_Date", SqlDbType.Date);
                com.Parameters.Add("@Subscriber_No", SqlDbType.BigInt);
                SqlParameter SqlParameter = new SqlParameter("@collected", SqlDbType.Decimal);
                SqlParameter.SourceColumn = "collected";
                SqlParameter.Precision = 18;
                SqlParameter.Scale = 3;
                com.Parameters.Add(SqlParameter);

                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {

                    com.Parameters["@Subscriber_No"].Value = dataGridView1.Rows[i].Cells[0].Value;
                    com.Parameters["@collected"].Value = dataGridView1.Rows[i].Cells[1].Value;
                    com.Parameters["@Payment_Date"].Value = dataGridView1.Rows[i].Cells[2].Value;
                }
                com.ExecuteNonQuery();

                MessageBox.Show("Successfully UPDATE....");

            }
        }

the sql server table :

Subscriber_No = bigint 
collected = numeric(18, 3)
company_name = nvarchar(50)
indebtedness_name = nvarchar(50)
Payment_Date = date

Edite

i use this code from @Caius Jard but i get error in the com.parameters date 'Object cannot be cast from DBNull to other types.'

this code

using (SqlConnection con = new SqlConnection("**"))
{
    con.Open();


    using (SqlCommand com = new SqlCommand("UPDATE indebtedness SET collected=@collected,Payment_Date=@Payment_Date WHERE Subscriber_No=@Subscriber_No and company_name=@company_name and indebtedness_name=@indebtedness_name", con))
    {
        com.Parameters.AddWithValue("@company_name", company_name.Text);
        com.Parameters.AddWithValue("@indebtedness_name", indebtedness_name.Text);
        com.Parameters.Add("@Payment_Date", SqlDbType.Date);
        com.Parameters.Add("@Subscriber_No", SqlDbType.BigInt);
        com.Parameters.Add(new SqlParameter("@collected", SqlDbType.Decimal) { Precision = 18, Scale = 3 } );

        int countSuccess = 0;
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {

            com.Parameters["@Subscriber_No"].Value = Convert.ToInt64(dataGridView1.Rows[i].Cells[0].Value);
            com.Parameters["@collected"].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
            com.Parameters["@Payment_Date"].Value = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value); //hope this is a date, not a string. If it's a string, parse it instead
            int numUpd = com.ExecuteNonQuery();
            countSuccess += numUpd;
        }


        MessageBox.Show($"Successfully UPDATED {countSuccess} of {dataGridView1.Rows.Count} rows" );

    }
}

Your statement

com.ExecuteNonQuery();

is outside the loop, because of that your query executes only for the last row in grid view and I assume you want to update all the rows using the loop.

so you have to execute com for each row

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    com.Parameters["@Subscriber_No"].Value = dataGridView1.Rows[i].Cells[0].Value;
    com.Parameters["@collected"].Value = dataGridView1.Rows[i].Cells[1].Value;
    com.Parameters["@Payment_Date"].Value = dataGridView1.Rows[i].Cells[2].Value;
    com.ExecuteNonQuery();
}
    using (SqlConnection con = new SqlConnection("**"))
    {
        con.Open();


        using (SqlCommand com = new SqlCommand("UPDATE indebtedness SET collected=@collected,Payment_Date=@Payment_Date WHERE Subscriber_No=@Subscriber_No and company_name=@company_name and indebtedness_name=@indebtedness_name", con))
        {
            com.Parameters.AddWithValue("@company_name", company_name.Text);
            com.Parameters.AddWithValue("@indebtedness_name", indebtedness_name.Text);
            com.Parameters.Add("@Payment_Date", SqlDbType.Date);
            com.Parameters.Add("@Subscriber_No", SqlDbType.BigInt);
            com.Parameters.Add(new SqlParameter("@collected", SqlDbType.Decimal) { Precision = 18, Scale = 3 } );

            int countSuccess = 0;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                com.Parameters["@Subscriber_No"].Value = Convert.ToInt64(dataGridView1.Rows[i].Cells[0].Value);
                com.Parameters["@collected"].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
                com.Parameters["@Payment_Date"].Value = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value); //hope this is a date, not a string. If it's a string, parse it instead
                int numUpd = com.ExecuteNonQuery();
                countSuccess += numUpd;
            }


            MessageBox.Show($"Successfully UPDATED {countSuccess} of {dataGridView1.Rows.Count} rows" );

        }
    }

If you see "updated 0.." then something is wrong with your search clauses (there is no row with that company_name and indebtedness_name)

If you see "updated X" where X is greater than 0 then updates were sent to the DB; check you're looking in the right DB

Note though that this code almost doesn't make sense unless there is a trigger doing something in the background - all it will do is update the same (set of) rows over and over again with new values, however many lines are in the grid. The final resting state will be as if you ran the last update line only, so there's no point if your grid has 500 lines, in running the first 499 updates (unless a trigger is capturing every change and doing something interesting with it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM