Hello all.
I am new to .net development.
I am inserting the data and i want the data to get displayed in the grid view as i enter the data.But it doesn't get displayed immediately.
If i refresh the page,the data gets inserted twice.
I tried to solve this using (!IsPostBack) property though i get the same bug.

My code is here:

try
                {
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    }
                    da.InsertCommand = new SqlCommand("insert into tbl_Comment values ('" + authenticate + "','" + txt_newitem.Text + "','" + txt_location.Text + "','" + date.ToString() + "','" + 1 + "')", cn);
                    da.InsertCommand.ExecuteNonQuery();
                    da.Update(ds);                                            
                    GridView1.DataBind();                      
                }
                catch (SqlException ex)
                {
                    lbl_error.Text = ex.Message;
                }
                finally
                {
                    cn.Close();
                }

Can any one guide me where i am making the mistake ?

Hello all.
I am new to .net development.
I am inserting the data and i want the data to get displayed in the grid view as i enter the data.But it doesn't get displayed immediately.
If i refresh the page,the data gets inserted twice.
I tried to solve this using (!IsPostBack) property though i get the same bug.

My code is here:

try
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
da.InsertCommand = new SqlCommand("insert into tbl_Comment values ('" + authenticate + "','" + txt_newitem.Text + "','" + txt_location.Text + "','" + date.ToString() + "','" + 1 + "')", cn);
da.InsertCommand.ExecuteNonQuery();
da.Update(ds);
GridView1.DataBind();
}
catch (SqlException ex)
{
lbl_error.Text = ex.Message;
}
finally
{
cn.Close();
}

Can any one guide me where i am making the mistake ?

Hi Priyanka

If your insert code is in page load event then wrap ur code under ispostback

if (!IsPostBack) 
//your insert code here
end if

regarding your second problem, show me the code which is retrieving data after insert action

Mark as solved if it hepls you!!!

it doesn't work either.
here is my modified code.

if (!IsPostBack)
                {
                    try
                    {
                        if (cn.State == ConnectionState.Closed)
                        {
                            cn.Open();
                        }
                        da.InsertCommand = new SqlCommand("insert into tbl_Comment values ('" + authenticate + "','" + txt_newitem.Text + "','" + txt_location.Text + "','" + date.ToString() + "','" + 1 + "')", cn);
                        da.InsertCommand.ExecuteNonQuery();
                        da.Update(ds);
                        GridView1.DataBind();
                        pnl_additem.Visible = false;
                        link_additem.Visible = true;
                        //Response.Redirect("default.aspx");
                    }
                    catch (SqlException ex)
                    {
                        lbl_error.Text = ex.Message;
                    }
                    finally
                    {
                        cn.Close();
                    }
                }

Post the code for your entire page. Please use code tags when posting code on daniweb:

[code]

...code here...

[/code]

Also use the debugger and evaluate IsPostback and see how many times the code is run.

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.