|
Try this example. I did this in one of my project. I generated the excel after reading the values from a GridView.
STEP 1:Create a function call DownLoad() to download the excel file
private void Download()
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/vnd.xls";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter objStringWriter = GenerateExcelBody();
Response.Write(objStringWriter.ToString());
Response.End();
}
STEP 2:Create a function called GenerateExcelBody() to generate the excel boby
private System.IO.StringWriter GenerateExcelBody()
{
System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);
string strVal =string.Empty;
for (int i = 0; i < grEims.Rows.Count; i++)
{
if (i % 2 == 0)
{
strVal += "<tr bgcolor='white'><td>" + grEims.Rows[i].Cells[0].Text + " " + grEims.Rows[i].Cells[1].Text + " " + grEims.Rows[i].Cells[2].Text.ToString() + " " + grEims.Rows[i].Cells[3].Text.ToString() + " " + grEims.Rows[i].Cells[4].Text.ToString() + " " + grEims.Rows[i].Cells[5].Text.ToString() + "</td></tr>" + Environment.NewLine;
}
else
{
strVal += "<tr bgcolor='white'><td>" + grEims.Rows[i].Cells[0].Text + " " + grEims.Rows[i].Cells[1].Text + " " + grEims.Rows[i].Cells[2].Text.ToString() + " " + grEims.Rows[i].Cells[3].Text.ToString() + " " + grEims.Rows[i].Cells[4].Text.ToString() + " " + grEims.Rows[i].Cells[5].Text.ToString() + "</td></tr>" + Environment.NewLine;
}
}
objHtmlTextWriter.WriteLine("<table width='100%'>" +
"<tr><td align=center bgcolor='#ccccff'><h4>List Of Interviews Conducted </h4></td></tr>" +
"<tr><td bgcolor='gray'><b>NO EMP NO. FIRST NAME LAST NAME RECORD RECEIVED ON INTERVIEW</b></td>" +
"</tr>" + strVal + "</table>");
return objStringWriter;
}
STEP 3: Call the Download() function from the control's event you want
e.g.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Download();
}
}
Hope this helps.
Let me know in case of any concern.
Niladri Biswas
|
|
|
|
|
It is useful for generating excel but u can see
when we open that excel and than save this excel
it displayed save as type web page.
can it display default save type as microsoft Excel?
|
|
|
|
|
How can i stop data saved twice when page is refreshed
|
|
|
|
|
|
1. Redirect your page on new location when data is successfully saved when user refresh the page it found new page refreshed which is not contains any sort of data saving code.
2. Clear all the state of controls after data saving and write state check condition before data save but I think first approach is better approach if you found any difficulty then I will send you sample code but remember if user go back to page and repeat same activity then you have to give dynamic image by that you always check random image code before saving same as in yahoo, google.
|
|
|
|
|
you just paste it below your class declaration
private bool _refreshState;
private bool _isRefresh;
public bool IsRefresh
{
get { return _isRefresh; }
}
protected override void LoadViewState(object savedState)
{
object[] allStates = (object[])savedState;
base.LoadViewState(allStates[0]);
_refreshState = (bool)allStates[1];
_isRefresh = _refreshState == (bool)Session["__ISREFRESH"];
}
protected override object SaveViewState()
{
Session["__ISREFRESH"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();
allStates[1] = !_refreshState;
return allStates;
}
and in button click
if (!IsRefresh)
{
//write your code here..
}
|
|
|
|
|
Hi All,
I am creating a job portal in asp.net2.0 and mysql as backend....
I want to create a drop down list.... which displays the list of states according to the country selected in the first dropdownlist by the user..
i have used the sqldatasource and wen i am inserting the following select query => "select statename from state where countryname=@countryname"... its not working for the mysql ... though its working very fine if i use the database as MSSQL ....
Plz help to solve this problem...
Thanks in Advance....
Regards,
Rajan Arora
|
|
|
|
|
You can copy this code from any website for example nokri.com which having all combo of this type but if you need it originally then lot of site provide complete list of same.
Code
1. Just fill all the entry into combo and Enable post back.
2. On Change of first combo fill the sate for country according to country name.
3. Then same for city
Table
CountryName, StateName, CityName
|
|
|
|
|
hi ,
I have a javascript function with 1 parameter[func(val)]
I'm trying to combine that param with some strings to get a regular expression
I tryed this:
var re = new RegExp(val + '.*')
and this:
var re = new RegExp('/' + val + '.*/')
it's not working...
|
|
|
|
|
benams wrote: it's not working...
And what does it has to do with ASP.Net? There is a better place[^](Web Development Forum) at CP for these type of questions.
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Could you please explain your problem in a more elegant way?
It will be easy for others to grasp and solve.
Niladri Biswas
|
|
|
|
|
Hi my fellow coders,
I'm trying to HTTP POST some data to a URL (API).
If I directly try on the browser, it works fine and giving the below response:
<?xml version="1.0" encoding="utf-8" ?>
<OUTPUT>
<STATUS>ERROR</STATUS>
</OUTPUT>
But If I use webclient class, the response is different, and is not posting the data at all:
<?xml version="1.0" encoding="utf-8"?>
<OUTPUT><STATUS>HTTP/1.0 200 OK
Content-Type: text/xml
Content-Length: 100
<?xml version="1.0" encoding="utf-8"?>
<OUTPUT><STATUS>SUCCESS</STATUS></OUTPUT></STATUS>
</OUTPUT>
Can my fellow experts give ma a hint on how should I mimic the browser results?
|
|
|
|
|
Give me code by that you are calling web service you are missing something.
You can also do remote debugging for your web service by that you found exact problem for remote debugging just attach process by which you are calling your web service if that is different machine then run webserive and put break point into web service when client call web service automatically execution stop on your break point.
|
|
|
|
|
Member 2102804 wrote:
If I directly try on the browser, it works fine and giving the below response:
<status>ERROR
How did you try on browser ?
Member 2102804 wrote: But If I use webclient class, the response is different, and is not posting the data at all:
<status>HTTP/1.0 200 OK
Content-Type: text/xml
Content-Length: 100
<status>SUCCESS
You want to mean, when you are posting the file from your client application ?
|
|
|
|
|
Hi...
Thank you for your response, FYI, it's not a webservice, it's a normal HTTP URL
Trying on browser, means typing the url in the browser's address, click below to try
http://gateway.ionnex.com:8001/sendsms.aspx[^]
(note this is not the url I'm testing as that url is only accesible via my server)
And I'm not posting file, just parameters and values.
I believe my client application is bug free as it works for other URL (API) but not this one, seems it's giving me the HTTP 200 status instead of the status from the server.
|
|
|
|
|
In my page, I have a datatable with duplicate data and I use the following code to make distinct column values.
private ArrayList RemoveDuplicatePartNumbers(DataTable dtParts)
{
ArrayList lstparts = new ArrayList();
DataTable dt1 = new DataTable();
//Assigning the table to DataView, which will remove duplicates after sorting.
DataView dv = new DataView(dtParts);
dv.Sort = "PartNumber ASC";
string[] strcolumns = { "PartNumber" };
dt1 = dv.ToTable(true, strcolumns);
for (int i = 0; i <= dt1.Rows.Count - 1; i++)
{
lstparts.Add(System.Convert.ToString(dt1.Rows[i]["PartNumber"]));
}
return lstparts;
}
Now my question is, can I reduce the above coding lines to some extent by using DataView.Select property or in some other way? Any thoughts? Please help.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|
|
Hi, you can fire a Linq query instead.
I have created a Datatable
public DataTable FunctionGetTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("PartNumber");
dt.Rows.Add("PartNumber1");
dt.Rows.Add("PartNumber5");
dt.Rows.Add("PartNumber6");
dt.Rows.Add("PartNumber3");
dt.Rows.Add("PartNumber3");
dt.Rows.Add("PartNumber6");
dt.Rows.Add("PartNumber3");
dt.Rows.Add("PartNumber5");
dt.Rows.Add("PartNumber5");
return dt;
}
My ArrayList function goes like this
private ArrayList RemoveDuplicatePartNumbers(DataTable dtParts)
{
ArrayList lstparts = new ArrayList();
var query = (from c in dtParts.AsEnumerable()
orderby c.Field<string>("PartNumber")
select c).Distinct(DataRowComparer.Default);
foreach (var d in query) lstparts.Add(d);
return lstparts;
}
Next call the function from any control's event(I have called from a button event)
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList RequiredList = new ArrayList();
RequiredList = RemoveDuplicatePartNumbers(FunctionGetTable());
}
Hope this helps
Niladri Biswas
modified on Wednesday, July 1, 2009 8:22 AM
|
|
|
|
|
Thanks a lot for your detailed reply. It worked!!! and I learned something about Link queries. Great Forum!!!
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|
|
Hi..
I have hosted my Asp.net application on Godaddy shared hosting environment. Now I want to upload images to my hosting server by using my code. So I used file upload control to upload my files, when I am trying to upload file on to my web server every time it's prompting window for user name password of my domain .can anyone tell me how can I upload a file on to my shared hosting environment without prompting authentication window.
Thanks in advance.
bah10z
|
|
|
|
|
You need to have appropriate permissions to write(upload) a file on the server.
Read this [^]on MSDN to understand what all permission you would need.
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
engbahramZarin wrote: when I am trying to upload file on to my web server every time it's prompting window for user name password of my domain .
Do you have the sufficient permission to upload the file ? is there any domain user name password you have for the hosting side?
|
|
|
|
|
I have four individual checkboxes in my form called as General, Direct, Indirect and Private. The issue is that when private is checked all others should be disabled and unchecked if checked. I have the following code for that:
if (PrivateCheckBox.Checked)
{
GeneralCheckBox.Checked = false;
GeneralCheckBox.Enabled = false;
IndirectCheckBox.Checked = false;
IndirectCheckBox.Enabled = false;
DirectCheckBox.Checked = false;
DirectCheckBox.Enabled = false;
}
else
{
GeneralCheckBox.Checked = true;
GeneralCheckBox.Enabled = true;
IndirectCheckBox.Checked = true;
IndirectCheckBox.Enabled = true;
DirectCheckBox.Checked = true;
DirectCheckBox.Enabled = true;
}
This serves the purpose but simply increases coding lines which I don't want. I can use the ternary operator, but only one operation can do with that, either checked or enabled. Can anybody have any idea how to achieve this with minimum line of code? I am not able to think of any other option.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|
|
GeneralCheckBox.Checked = GeneralCheckBox.Enabled = !PrivateCheckBox.Checked;<br />
IndirectCheckBox.Checked = IndirectCheckBox.Enabled = !PrivateCheckBox.Checked;<br />
DirectCheckBox.Checked = DirectCheckBox.Enabled = !PrivateCheckBox.Checked;
only two letters away from being an asset
|
|
|
|
|
Add the check boxes (except private check box ) to a checkbox group. and use this code
foreach (CheckBox ch in CheckBoxList1.Items)
{
if(private.checked)
{
ch.Checked = false;
ch.Enabled = false;
}
else
{
ch.Checked = true;
ch.Enabled = true;
}
}
If you want to do similar kind of operation to a group of items then this is the standard way of doing that. Hope this help
|
|
|
|
|
foreach (control ctrl in page)
{
if(ctrl is checkbox)
{
((checkbox)(ctrl)).enabled = false;
((checkbox)(ctrl)).checked = flase;
}
}
|
|
|
|