sumit
sumit

Reputation:

Need help in gridview displaying data

I want to display all the data in gridview which i am inserting in runtime. For that i have written code but getting this error.

"Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindData();
    }
}

public void BindData()
{
     string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
     SqlConnection con = new SqlConnection(str);
     SqlDataAdapter da = new SqlDataAdapter("select * from Items where ItemId='" + TxtItemId.Text + "'", con);
     DataSet ds = new DataSet();
     da.Fill(ds,"Items");
     GridView1.DataSource = ds;         
     GridView1.DataBind();
}

Pls modify my code where is my mistake.

Thanks, Sumit

Upvotes: 0

Views: 844

Answers (4)

Shantanu Sinha
Shantanu Sinha

Reputation: 5

For inserting data on button click

SqlConnection con = new SqlConnection("connection string");
        SqlDataAdapter myadp = new SqlDataAdapter();
        myadp.InsertCommand = new SqlCommand();
        myadp.InsertCommand.Connection = con;
        con.Open();
        myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
        myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
        int i = myadp.InsertCommand.ExecuteNonQuery();
        if (i == 1)
        {
           // Response.Write("<script>alert('number updated sucessfully')</script>");
            Label2.Text = "Number updated sucessfully".ToString();
            TextBox10.Text = "";
        }
        else
        {
            //Response.Write("<script>alert('somthing went wrong try again')</script>");
            Label2.Text = "somthing went wrong try again".ToString();
        }
        con.Close();
        Responce.Redirect("your page.aspx");

To display data on the grid view

SqlCommand command = new SqlCommand("SELECT * from number");
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind(); 

so the overall coding will be

SqlConnection con = new SqlConnection("connection string");
        SqlDataAdapter myadp = new SqlDataAdapter();
        myadp.InsertCommand = new SqlCommand();
        myadp.InsertCommand.Connection = con;
        con.Open();
        myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
        myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
        int i = myadp.InsertCommand.ExecuteNonQuery();
        if (i == 1)
        {
           // Response.Write("<script>alert('number updated sucessfully')</script>");
            Label2.Text = "Number updated sucessfully".ToString();
            TextBox10.Text = "";
        }
        else
        {
            //Response.Write("<script>alert('somthing went wrong try again')</script>");
            Label2.Text = "somthing went wrong try again".ToString();
        }
        con.Close();
        Responce.Redirect("your page.aspx");
To display data on the grid view

    SqlCommand command = new SqlCommand("SELECT * from number");
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();



here the Responce.Redirect("your page.aspx"); will do the trick for you 

Upvotes: 0

Chandan Kumar
Chandan Kumar

Reputation: 4638

You need to use SqlParameter with sql command.

and you are missing logic. First when form loads at that moment you need to bind the complete Item Table data to your Grid, Ten you would have a textbox and button where you want to filter the grid.

Here is your Complete Code . Use it. 100% works.

    <div id = "dvFilterGrid">
       Filter Grid :  <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnFilterGrid" runat="server" Text="Find" 
            onclick="btnFilterGrid_Click" />
    </div>

and your code behind

public partial class _Default : System.Web.UI.Page
{
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridData();
        }
    }

    protected void BindGridData()
    {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("Select * from Items", con))
        {
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

    protected void btnFilterGrid_Click(object sender, EventArgs e)
    {
        int ItemId = Convert.ToInt32(txtFilter.Text.Trim());
        FilterData(ItemId);
    }

    private void FilterData(int ItemId)
    {
        string query = "select * from Items where ItemId=@ItemId ";
         SqlCommand cmd = new SqlCommand(query,con);
         cmd.Parameters.Add(new SqlParameter("@ItemId", ItemId));
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         DataSet ds = new DataSet();
         da.Fill(ds,"Items");
         GridView1.DataSource = ds;         
         GridView1.DataBind();
    }
 }

Upvotes: 0

Bogdan Maxim
Bogdan Maxim

Reputation: 5946

The two properties are mutually exclusive (when you use one of them, you are not allowed to use the other one):

  1. The DataSourceID property is to be used with a DataSource control (ObjectDataSource, XmlDataSource, SqlDataSource, etc)
  2. The DataSource property is to be used with custom objects / custom data binding.

Examples:

  1. DataSourceId

    @asp:ObjectDataSource ID="ods1" runat="server" SelectMethod="Test" TypeName="TestBL" /@ @asp:GridView ID="gv1" runat="server" DataSourceID="ods1" /@

in this case databinding occurs automatically

  1. DataSource

    @asp:GridView ID="gv2" runat="server" %@

and in code behind you would have something like this:

overrides void OnLoad(..)
{
    List<DataObject> source = new List<DataObject>();
    source.Add(new DataObject(..));
    source.Add(new DataObject(..));
    source.Add(new DataObject(..));
    gv2.DataSource = source;
    gv2.DataBind();
}

Please note that I have used @ instead of angular brackets.

Upvotes: 1

Canavar
Canavar

Reputation: 48088

It seems like you want to bind your datasource manually. Remove the DataSourceID property at the design view from your gridview. It should be something like that :

<asp:GridView runat="server" ID="grid" DataSourceID="SqlDataSource1"></asp:GridView>

Just remove the DataSourceID="SqlDataSource1" property.

Upvotes: 0

Related Questions