populate GridView with stored procedure
-
HI! I do not know how to populate my GridView with stored procedure I have this code but it doesn't works she returned result or error message
public void BindData()
{
SqlConnection myConnection = new SqlConnection(ConnectionString);SqlCommand myCommand = new SqlCommand("PS\_NV2", myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); myAdapter.Fill(ds, "TAble"); myConnection.Open(); myCommand.ExecuteNonQuery(); GridView2.DataSource = ds; try { GridView2.DataBind(); } catch { GridView2.PageIndex = -1; GridView2.DataBind(); } myConnection.Close();
correct me please.Thanks
-
HI! I do not know how to populate my GridView with stored procedure I have this code but it doesn't works she returned result or error message
public void BindData()
{
SqlConnection myConnection = new SqlConnection(ConnectionString);SqlCommand myCommand = new SqlCommand("PS\_NV2", myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); myAdapter.Fill(ds, "TAble"); myConnection.Open(); myCommand.ExecuteNonQuery(); GridView2.DataSource = ds; try { GridView2.DataBind(); } catch { GridView2.PageIndex = -1; GridView2.DataBind(); } myConnection.Close();
correct me please.Thanks
execute the executenonquery before putting into adapter.
-
execute the executenonquery before putting into adapter.
Thank you Satish Mahapatra. it doesn't works :( this code works fine for a datagrid but not for the GridView I do not know if the error can come from here for datagrid i have DataGrid1.CurrentPageIndex = 0; I have replaced by : GridView2.PageIndex =0 ; thanks.
-
Thank you Satish Mahapatra. it doesn't works :( this code works fine for a datagrid but not for the GridView I do not know if the error can come from here for datagrid i have DataGrid1.CurrentPageIndex = 0; I have replaced by : GridView2.PageIndex =0 ; thanks.
can u send me whole code and where exactly ur getting the error with error description.
-
can u send me whole code and where exactly ur getting the error with error description.
I have no erreur no message erreur if i click sur the button I havent result. my code c#:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; private void Page_Load(object sender, System.EventArgs e) { } public void BindData() { SqlConnection myConnection = new SqlConnection(ConnectionString); SqlCommand myCommand = new SqlCommand("PS_NV2", myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); myAdapter.Fill(ds, "table"); myConnection.Open(); myCommand.ExecuteNonQuery(); GridView2.DataSource = ds; try { GridView2.DataBind(); } catch { GridView2.PageIndex = 0; GridView2.DataBind(); } myConnection.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=**3*;Initial Catalog=mybd"); return connectionString; } } protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { Response.Write(e.NewSelectedIndex); } protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = GridView2.SelectedRow; TextBox4.Text = row.Cells[2].Text; } protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; BindData(); } protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; BindData(); } protected void Button1_Click1(object sender, EventArgs e) { BindData(); GridView2.Visible = true; } }
DataKeyNames="c1" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowEditing="GridView2_RowEditing" OnSelectedIndexChanged="GridView2_SelectedIndexChan
-
I have no erreur no message erreur if i click sur the button I havent result. my code c#:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; private void Page_Load(object sender, System.EventArgs e) { } public void BindData() { SqlConnection myConnection = new SqlConnection(ConnectionString); SqlCommand myCommand = new SqlCommand("PS_NV2", myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); myAdapter.Fill(ds, "table"); myConnection.Open(); myCommand.ExecuteNonQuery(); GridView2.DataSource = ds; try { GridView2.DataBind(); } catch { GridView2.PageIndex = 0; GridView2.DataBind(); } myConnection.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=**3*;Initial Catalog=mybd"); return connectionString; } } protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { Response.Write(e.NewSelectedIndex); } protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = GridView2.SelectedRow; TextBox4.Text = row.Cells[2].Text; } protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; BindData(); } protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; BindData(); } protected void Button1_Click1(object sender, EventArgs e) { BindData(); GridView2.Visible = true; } }
DataKeyNames="c1" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowEditing="GridView2_RowEditing" OnSelectedIndexChanged="GridView2_SelectedIndexChan
i told u to put that executenonquery statement before putting into adaptare.now u just debug the code and see in the binddata method what it returns in the dataset and tell me.
-
i told u to put that executenonquery statement before putting into adaptare.now u just debug the code and see in the binddata method what it returns in the dataset and tell me.