Both DataSource and DataSourceID are defined on 'GridView2'. Remove one definition - c#

I am trying to reload a gridview upon date selected from calendar.
I know there are duplicate questions on SO but their answers did not work for me
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = Calendar1.SelectedDate.ToShortDateString();
DataSet ds = dlObj.FillDataSet("SELECT top 5 [DName], [bloodGroup], [dateDonated] FROM [tblDonors] ORDER BY [dateDonated] DESC ", "tblDonors");
GridView2.DataSource = ds;
GridView2.DataBind();
}
And the method FillDataSet() is this
public DataSet FillDataSet(string q, string tableName)
{
DataSet ds = new DataSet();
try
{
SqlDataAdapter da = new SqlDataAdapter(q, thisConnection);
da.Fill(ds, tableName);
return ds;
}
catch (Exception)
{
return ds;
}
}
When I click any date, this error occurs
Both DataSource and DataSourceID are defined on 'GridView2'. Remove one definition.

DataSourceID="ds"
This is not needed when you are adding datasource from code behind.

While, in general, you should really decide on whether to use design side datasource or use code behind, there may be occasions where it is preferable to use both. One way to cheat a bit, in code behind use:
grid.DataSourceID = null;
grid.DataSource = dataTable;
grid.DataBind();

In Calendar1_SelectionChanged you should simply call
GridView2.DataBind()
and handle whatever it is you are trying to do in the
DataSourceID_Selecting
event by updating e.Result

Related

Add selected rows from one GridView to another GridView

I need to add the selected rows from one GridView to another GridView in ASP.NET C#.
So the code i was implemented on my project to copy selected rows from one Grid to another Grid.
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection(#"Data Source=.\sqlexpress;Initial Catalog=x;Integrated Security=True");
sc.Open();
SqlDataAdapter sd = new SqlDataAdapter("select * from BookDetails", sc);
DataSet ds = new DataSet();
sd.Fill(ds, "BookDetails");
sc.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
GridView2.DataSource = row; //error here
GridView2.DataBind();
}
when i run the project i got an error (GridView2.DataSource = row;) :-
"Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource."
Image of what i need to implement
error of the code
Have you tried typecasting "row" to be "IListSource, IEnumerable, or IDataSource."? If that doesnt work, it may be because GridView2.DataSource can only be IListSource, IEnumerable, or IDataSource thus meaning it simply isn't possible. I don't fully understand what you're trying to do in the function "GridView1_SelectedIndexChanged1".

show header and footer in gridview asp.net when data source return no data

Goal
show header and footer when Sqldatasource of GridView1 returns no data
Problem
now this is what i did so far i have a GridView in my asp.net application which shows meetings of a persons stored in database
in footer of GridView i added functionality of adding new meeting for person
but problem arise when a person don't have meetings stored in database data source bounded to gridview return no data and there goes my functionality of adding new meeting because GridView does not display until they have a row
any solution i can show header but how to show footer when GridView have no data
i dont want to use EmptyDataTemplate because i have to reimplement add new meeting functionality (if i am right)
thanks in advance
Here while binding datatable to gridview datasource , you can check number of rows and depend on row count you can set datasource to your gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
bindGV();
}
}
public void bindGV() {
SqlDataAdapter dap = new SqlDataAdapter("YourSQlQuery ", cn);
DataSet ds = new DataSet();
dap.Fill(ds);
DataTable myDt = ds.Tables[0];
if (myDt.Rows.Count == 0)
{
myDt.Rows.Add(myDt.NewRow());
GridView1.DataSource = myDt;
GridView1.DataBind();
GridView1.Rows[0].Visible = false;
}
else
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}

asp.net foreach row in dataset make a balloon with text

I got a question for you guys.
How do I Dynamicly add an item to asp.net?
there a way to make a balloon looking box of text?
I know how to get items into ds from database and fill it up and read it for each row there is. but that does not help me alot when I want to make dynamicly some stuff and get a ballon view of text just like if you are looking at your phone and see the text messeges thats how I want it to look like.
I took a example of my code where I show how I fill the dataset and how I bind the data into a Gridview
DBControl.cs
public DataSet GetData(String queryString)
{
DataSet ds = new DataSet();
try
{
//run the query.
SqlDataAdapter adapter = new SqlDataAdapter(queryString, m_helpdeskconnection);
// Fill the DataSet.
adapter.Fill(ds);
}
catch
{
// The connection failed. Display an error message.
//Message.Text = "Unable to connect to the database.";
}
return ds;
}
AdministrerBrugere.aspx.cs
DBControl db = new DBControl();
String queryString;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
queryString = "SELECT KundeNr, Navn FROM Kunder";
BindData();
}
}
private void BindData()
{
// Run the query and bind the resulting DataSet
// to the GridView control.
db.ConnOpenHelpdesk();
DataSet ds = db.GetData(queryString);
GridView1.DataSource = ds;
GridView1.DataBind();
db.ConnCloseHelpdesk();
}
You could use a DataRepeater to bind your content and use an ItemTemplate that has a CSS styled DIV to make it look like a baloon.
Try these links:
http://www.w3schools.com/css/css3_borders.asp
http://www.w3schools.com/aspnet/aspnet_repeater.asp

Display tables from SQL in DataGridView

I created a DataBase named charityah containing 5 tables. Their names are listed in a combobox.
When I choose one of them I want to display their content in a DataGridView.
What I tried is: first I linked the DataGridView to this database and tried this code that I found:
SqlConnection connection = new SqlConnection();
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = comboBox1.Text;
connection.ConnectionString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True";
using (connection)
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from "+s, connection);
DataSet ds = new DataSet();
adapter.Fill(ds, s);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Refresh();
}
}
This method doesn't give me any errors and it finds the tables, but nothing is seen in the DataGridView.
Since you report (comments) that there are rows, it sounds like the primary problem (connection disposal aside) is an issue with data-binding. A few thoughts leap to mind:
is the table in virtual mode?
is it adding columns?
do the columns already exist?
You might want to try adding:
dataGridView1.VirtualMode = false;
dataGridView1.Columns.Clear();
dataGridView1.AutoGenerateColumns = true;
before the:
dataGridView1.DataSource = ds.Tables[0];
You might also want to check that dataGridView1.DataMember doesn't have a value assigned.
try this, some times nonsense items do create a lot of mess. so try setting autogenerate columns to true. may this starts showing you the results. because as per your comments, it dosent seems there could be any other issue. so just give it a try
dataGridView1.AutoGenerateColumns = true;

ASP.NET why Gridview Paging on page 2 and the rest of it are disappear?

I have so many list of ServerName in GridView, so I decide to add paging. The data show list result on page 1 but on page 2 and the rest of it is not display anything. I already have OnPageIndexChanging="GridViewServer_PageIndexChanging" in GridViewServer property. Please help! Here is c# code behind,
protected void GridViewServer_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewServer.PageIndex = e.NewPageIndex;
GridViewServer.DataBind();
}
A GridView binding function codes,
public void BindGridView()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
conn.Open();
string sqlquery = ("SELECT * FROM tblServer");
SqlCommand command = new SqlCommand(sqlquery, conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adp.Fill(ds);
GridViewServer.DataSource = ds.Tables[0];
GridViewServer.DataBind();
}
You need to set your GridView's datasource appropriately. You can't just call DataBind if the datasource isn't properly set. Basically what it amounts to is binding your GridView to null (which would have no page 2). I would recommend having a private method that is responsible for this process and whenever you need to bind you call that.
private void BindGridViewServer()
{
GridViewServer.DataSource = GetYourData(); // This should get the data
GridViewServer.DataBind();
}
Call this method from within your event with:
protected void GridViewServer_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewServer.PageIndex = e.NewPageIndex;
BindGridViewServer();
}
This could be made more extensible by passing in the GridView as a parameter, but you'd have to have other parameters as well to make sure the method retrieves the proper data.
Here's a very good tutorial (with sample code) on custom GridView paging. This makes the paging controls look like the familiar ones you see on a lot of search engines, forums, etc.
http://geekswithblogs.net/aghausman/archive/2009/05/18/custom-paging-in-grid-view.aspx
You have to give datasource to GridViewServer every time on page index changed.
so the code would be like this
protected void GridViewServer_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewServer.PageIndex = e.NewPageIndex;
GridViewServer.Datasource = MethodReturningDataTable();
GridViewServer.DataBind();
}

Categories

Resources