Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,
I am using C# Windows Application.I am displaying SQL data into grid view using for loop Condition.Now, I want to update data(which is display in datagridview) into SQL Database...But it didn't display the newly added data...
here is my sample code Display data in Grid....

C#
comm = "select Name,Rate,Qty,TotalAmount from SALESENTRY where PENo = '" + txt_VocherNo.Text + "'";

                SqlCommand sqlcom = new SqlCommand(comm, sqlcon);
                SqlDataReader read = sqlcom.ExecuteReader();
                if (read.HasRows)
                {
                    int k = 0;
                    while (read.Read())
                    {
                        dgv_Entry.Rows[k].Cells["ColumnName"].Value = read.GetString(0);
                        dgv_Entry.Rows[k].Cells["ColumnRate"].Value = read.GetString(1);
                        dgv_Entry.Rows[k].Cells["ColumnRQty"].Value = read.GetString(2);
                        dgv_Entry.Rows[k].Cells["ColumnTotal"].Value = Total;
                        k++;
                    }
                }
                sqlcon.Close();


And its MY Update Code..

C#
if (btn_save.Text.Trim() == "&Update")
                        {                                                         
               for (int no = 0; no <= dgv_Entry.Rows.Count - 1; no++)
                {
                    if (dgv_Entry.Rows[no].Cells["ColumnName"].Value.ToString() == "" || dgv_Entry.Rows[no].Cells["ColumnName"].Value.ToString() == null)
                    { break; }
                    else
                    {
                        VNo = txt_VocherNo.Text;                        
                        Name = dgv_Entry.Rows[no].Cells["ColumnName"].Value.ToString();
                       SRate = dgv_Entry.Rows[no].Cells["ColumnRate"].Value.ToString();
                       RQty = dgv_Entry.Rows[no].Cells["ColumnRQty"].Value.ToString();
                       Total = dgv_Entry.Rows[no].Cells["ColumnTotal"].Value.ToString();
                      Cmmd = "Update SALESENTRY Set Name = '" + Name + "',SRate ='" + Rate + "',RQty='" + Qty + "',Total='" + TotalAmount + "' Where Vno = '"+Vno+"'
            DbCon.SetDataBase(Cmmd);
                    }
                }
                                MessageBox.Show("Record Updated", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);


Now How can i update Newly Added record Instead of Old
For Example.,I have Already 4 Records, now i want to Add one More Record using Update Button..How can i update?..
Please Suggest...
Posted
Updated 29-Jun-13 21:47pm
v2
Comments
Software Engineer 892 30-Jun-13 5:57am    
my dear friend,

for Adding a new record. you should use add button and also Use INSERT Query.

So, New record will be Inserted. using INSERT QUERY.

Code must be written in Gridview RowCommand event.

I think you have understood.

1 solution

Hey there,

See i get that you want Insert & Update to performed at the same time(on 1 click event),
So i will just explain the logical ways to do it. as below

1) Before you INSERT or UPDATE check for the existence of Primary (INDEX) of the row
(in your grid view)

If (Your row ID exists<using select=""> )
Execute UPDATE_SQL_QUERY
else
Execute INSERT_SQL_QUERY

---------
Example for Students_Table
Check if (Students_Table.Roll is present)
Update Name = 'Jim carry'
Else
Insert Student with name new 'Tom Cruise'
 
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