Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to update table column in table using datagridview and how to insert numeric value in access database via datagridview

$
0
0

Hi all,

    I have two queries 

(1) i want to insert numeric value into access database via datagridview but its always inserting 0 into database i tried everything but still it is inserted as 0 following code is for inserting i use debuger also but its not going inside     cmd.Parameters.AddWithValue("@total_available", Availability); 

    private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        { if (dataGridView1.IsCurrentRowDirty)
            {

int Availability;
                bool Total_available =Int32.TryParse (dataGridView1.Rows[e.RowIndex].Cells["total_available"].Value.ToString(),out Availability);
                if (Total_available)
                {
                    cmd.Parameters.AddWithValue("@total_available", Availability);
                }

}

}

  

(2) im trying to update only one table column via datagridview , like suppose in datagridview user choose medicine name XYZ then its total available column update (user define value)-1 , in datagridview im not showing total_available column i only update by medicine name but its showing me 

                                         

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index exception on  string medicinename = dataGridView1.SelectedRows[1].Cells["Medicine_Name"].Value.ToString();  this line

  private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;
string medicinename = dataGridView1.SelectedRows[1].Cells["Medicine_Name"].Value.ToString();


            DialogResult dialogResult = MessageBox.Show("Are you sure you want to insert data", "Data insert Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.Yes)
            {

                cmd = new OleDbCommand("update Medicine_Available_Detail set [total_available]=[total_available]-1 where [Medicine_Name]=@Medicine_Name", con);

cmd.Parameters.AddWithValue("@Medicine_Name", medicinename);
                con.Open();
                int n = cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Record Updated Successfully");
                showdata();
                dataGridView1.Refresh();



            }

pls friends help to solve this two problems 


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>