how do i display the inserted record in gridview in asp.net when i click on button..?

GridView1.DataSource = a.filldata("insert into student(Roll_no,Name,Age,Education,Mobile)values("+txtrollno.Text +",'"+txtname.Text +"',"+txtage.Text +",'"+txtedu.Text +"',"+txtmb.Text+")");
GridView1.DataBind();

i want it should be display right now at runtime record should be added....and displayed...
and i also get and error of "," i dont know how...and its of nonexecutequery()

update := GridView1.DataSource = a.filldata("update student set Roll_no="+txtrollno.Text +"where Roll_no=" + txtrollno.Text + ",Name='" + txtname.Text + "',Age=" + txtage.Text + ",Education='" + txtedu.Text + "',Mobile=" + txtmb.Text + ")");
GridView1.DataBind();

plzz help...

Dani AI

Generated

Short answer: run the INSERT (or UPDATE) with a command that does not expect rows, then re-query the table and bind that result to the GridView. The code in the original post is trying to feed an INSERT/UPDATE into a method that looks like it returns rows, which causes syntax and runtime errors.

Example pattern to follow (use parameterized SQL and using blocks):

/* Insert then refresh the grid */
using (var conn = new SqlConnection(connString))
using (var cmd = conn.CreateCommand())
{
    cmd.CommandText = "INSERT INTO Student (Roll_no, Name, Age, Education, Mobile) VALUES (@roll,@name,@age,@edu,@mobile)";
    cmd.Parameters.Add("@roll", SqlDbType.Int).Value = int.Parse(txtRollNo.Text);
    cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = txtName.Text;
    cmd.Parameters.Add("@age", SqlDbType.Int).Value = int.Parse(txtAge.Text);
    cmd.Parameters.Add("@edu", SqlDbType.NVarChar).Value = txtEdu.Text;
    cmd.Parameters.Add("@mobile", SqlDbType.NVarChar).Value = txtMobile.Text;
    conn.Open();
    cmd.ExecuteNonQuery();
}
BindGrid();
/* BindGrid reads the current rows and calls DataBind */
private void BindGrid()
{
    var dt = new DataTable();
    using (var conn = new SqlConnection(connString))
    using (var da = new SqlDataAdapter("SELECT * FROM Student ORDER BY Roll_no", conn))
    {
        da.Fill(dt);
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

Common gotchas and fixes:

  • Use ExecuteNonQuery for INSERT/UPDATE/DELETE; use a SELECT (DataAdapter.Fill/ExecuteReader) to get rows for binding.
  • The UPDATE syntax must be UPDATE Student SET Col1=@v1, Col2=@v2 WHERE Key=@key — the WHERE clause is not separated by commas.
  • Always use parameters to avoid quoting errors and SQL injection; convert text to ints where needed.
  • Wrap DB calls in try/catch and validate user input before converting types.
  • Consider setting GridView.DataKeyNames = "Roll_no" and using a dedicated bind method so the UI refresh is reliable.

As hinted, make sure the SET/WHERE order is correct. For GridView basics and pointed to resources — the missing piece here is the difference between commands that return rows and those that only execute. For reference, see SqlCommand.ExecuteNonQuery and SqlDataAdapter.Fill in the Microsoft docs:
SqlCommand.ExecuteNonQuery

Recommended Answers

All 4 Replies

In my query's i do UPDATE X SET Y = '"+ blah.text + "',Z='" + bzla.text...... hope that helps a bit... more code would help me see better.

thnx

Hi

Generally GridView is used to show data in tabular format. It also provide ways to modify and delete records.

If you want to more detail about it and want to get code for display record than just go for below link.

http://www.dotnetfunda.com/articles/article180.aspx

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.