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

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

Related

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

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

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

Refresh GridView After Data is Deleted From DetailView Using C#

When user select any record from the GridView then my DetailView is updated based on the selection of the GridView. So what I am trying to do is that when I delete anything from the DetailView then I want to refresh the GridView so basically I don’t want to show still the deleted record in the GridView. I have tried to resolve this issue by doing the data bind after my connection and SQL statement but it does not refresh it. One thing to note is that I am using a Accordion pane but both my gridview and the detailview are on the same pane. I am not sure if this is breaking anything. Here is my code:
protected void Refresh_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName.Equals("Delete", StringComparison.CurrentCultureIgnoreCase))
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select ID, Name, Address from dbo.MyTable", con);
DataTable dt = new DataTable();
da.Fill(dt);
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
}
You may use the event of the Data view called "ItemDeleted" as follows:
DetailViewName_ItemDeleted(object sender,
DetailsViewDeletedEventArgs e)
{
// Refresh the GridView control after a new record is updated
// in the DetailsView control.
GridViewName.DataBind();
}
The above code is from the official MSDN site for detail view control.
The other way (which I prefer) is to handle the data grid during the Page_load procedure so when you press your delete button in the detail view, the page will perform a postback.
So in the procedure Page_load you can call another procedure which fills the data grid. The code might be like this:
if (isPostback)
{
FillGrid();
}
private void FillGrid()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select ID, Name, Address from dbo.MyTable", con);
DataTable dt = new DataTable();
da.Fill(dt);
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
Your code doesn't show anything where the record is actually deleted so it's understandable that when you re-fetch the data still contains everything.
What you need to do is:
Execute a sql statement to delete the record
Re-fetch the data
Rebind the data.
If you follow those 3 steps it will work.
try to use the event that is called in case of pressing the delete key from the DGV
private void DGV_DeleteKeyPressed(object sender, KeyEventArgs e)
{
//enter code here
}

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

Import data from a SQL DB Table's Column (a DataSet) into a TextBox?

Using: ASP.NET 4, SQL 2008, C#
I'm writing a web app for our projects team to keep track of special project users. One thing they have asked is to export quick lists of data that they use often. For instance, what I'm trying to do here is give the team a button which will populate a TextBox with all the various email addresses that are saved in our SQL DB.
What I've been trying to do is bind the TextBox to a specific column in the DB. I already have a connection to the DB, and I've built a DataSet that queries for that specific column, and it works normally.
My question is, how do I tell ASP.NET to use the data from the DataSet to populate the Textbox? Here is the code I'm working with so far:
protected void btnGetNPMEmailAddresses_Click(object sender, EventArgs e)
{
txbResults.Text = "";
txbResults.DataBind(ObjectDataSource_Designee_EmailAddresses) // ??? My DataSet
}
I'm definitely crawling my way up the C# learning curve. Any help would be greatly appreciated!
Thanks,
Paul D.
Try something like this:
protected void btnGetNPMEmailAddresses_Click(object sender, EventArgs e)
{
// TODO:: Populate ObjectDataSource_Designee
DataSet dataSet = GetDS(ObjectDataSource_Designee);
string emails = string.Empty;
for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
{
emails = emails + dataSet.Tables[0].Rows[i]["EmailAddresses"].ToString() + Environment.NewLine;
}
txbResults.Text = emails;
}
private DataSet GetDS(ObjectDataSource ods)
{
var ds = new DataSet();
var dv = (DataView)ods.Select();
if (dv != null && dv.Count > 0)
{
var dt = dv.ToTable();
ds.Tables.Add(dt);
}
return ds;
}
You can't databind to a textbox in ASP.Net.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox_properties
There is no DataSource property. You can manually go through and grab what's in the dataset and put it into the textbox, but not bind it. Do you mean textbox?
Assuming you are returning only one row and col from the database, you can do something like below:
txbResults.Text = dataSet.Tables[0].Rows[0].Cols[0].ToString();
My suggestion would be to use "ExecuteScalar()" method that returns only one value instead of using DataAdapter/Dataset.
Hope this Helps!!
I would say
foreach(DataRow dr in DataTable)
{
Textbox.Text = Textbox.Text + dr;
}
if for some reason you HAVE to use a text box

Categories

Resources