0

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 ?

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by sknake
0

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!!!

Edited by reach_yousuf: n/a

0

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();
                    }
                }

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.