|
I tried to export datagrid data to the excel. I got problem while exporting data to excel while working in IIS.It is showing COM file is missing.Please help me out
Haripriya
|
|
|
|
|
Hi,
Can you explain how you are exporting data to excel....It will help to get ur problem solve quickly..
If u want you can use the below code for exporting the data in excel.....
private void btnExportToExcel_Click(object sender, System.EventArgs e)
{
dataGrid1.DataSource=TT_Reports.getDefaultersList(DateTime.Parse(TxtFromDate.Text),DateTime.Parse(TxtToDate.Text ),ddnTeamList.SelectedValue.ToString());
dataGrid1.DataBind();
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Response.AddHeader("content-disposition", "attachment;filename=DefaultersList_"+DateTime.Today.ToShortDateString()+".xls");
this.ClearControls(dataGrid1);
dataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
private void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
|
|
|
|
|
Ya i used this code which u have pasted but it is not for multisheets of excel.Thats y i tried like this
Dim obj_xl As New Excel.Application()
Try
obj_xl = GetObject(, "Excel.Application")
Catch ex As Exception
obj_xl = GetObject("", "Excel.Application")
End Try
obj_xl.Application.Visible = True
obj_xl.Workbooks.Add()
obj_xl.Sheets("Sheet1").Select()
With obj_xl
For i = 0 To dt.Rows.Count - 1
For j = 0 To dt.Columns.Count - 1
If IsDBNull(dt.Rows(i).Item(j)) Then
.ActiveCell(i + 4, j + 1) = String.Empty
Else
.ActiveCell(i + 4, j + 1) = dt.Rows(i).Item(j) End If
Next
Next
obj_xl.Dialogs(Excel.XlBuiltInDialog.xlDialogSaveAs).Show()
End With
This was the method i using to export values from data table.
I used some COM components for excel
This worked fine in my system.When i try to run in IIS its showing error as COM file missing
|
|
|
|
|
Use This Code:
protected void bttbexcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter stringWrite);
GridView1.RenderControl(htmlWrite);
this.EnableViewState = false;
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.Attributes["runat"] = "server";
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
|
|
|
|
|
Sneha Bisht wrote: Use This Code:
protected void bttbexcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter stringWrite);
GridView1.RenderControl(htmlWrite);
this.EnableViewState = false;
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.Attributes["runat"] = "server";
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
The Excel is not true excel.
|
|
|
|
|
I am adding controls to a placeholder on the fly.
I could have any number of controls in there but its usually about 5.
I am adding a label and a textbox to the placeholder.
Now when I loop the placeholder I have the following code:
The textbox value works fine it finds the correct value, but I cant find the label value, why is this?
Any help would be great.
thanks
PlaceHolder placeHolder1 = (PlaceHolder)FindControl("PlaceHolder2");
foreach (Control c in PlaceHolder2.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
Label lb2 = (Label)c;
TextBox tb2 = (TextBox)c;
SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
cmdinsert.ExecuteNonQuery();
}
}
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
modified on Wednesday, October 8, 2008 5:39 AM
|
|
|
|
|
The code
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
spessifies that it only goes into the statment if it is an TextBox
try
Label lb2;
TextBox tb2;
foreach (Control c in PlaceHolder2.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
TextBox tb2 = (TextBox)c;
}
else if(c.GetType().ToString().Equals("System.Web.UI.WebControls.Label"))
{
lb2 = (Label)c;
}
}
}
SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con); cmdinsert.ExecuteNonQuery();
|
|
|
|
|
But I need the sql statement to be in the loop, will this still work?
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
Are you adding multiple textboxes and labels to the placeholder?
In that case you need to know witch of the labels og textboxes pair you wan't to insert.
|
|
|
|
|
When I use the above code I get the following error:
CS0165: Use of unassigned local variable 'lb2'
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
try
Label lb2 = new Label();
TextBox tb2 = new TextBox();
foreach (Control c in PlaceHolder2.Controls)
{...
|
|
|
|
|
OK thanks
That now has got rid of the error. The problem is the sql statement doesnt do anything. If I hard code the value in for the label and the line of code for the label out it works. Seems weird, as if the format of the label is not correct.
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
The problem appears to be with the if statement. If I use label in the if statement and hardcode the textbox value it works and if I use the textbox in the if statement and hardcode the label it also works. it seems that I cant find the label and textbox values if Im using the if statement below, is there a way around it?
thanks so far
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
if you know the ID of the label and the textbox, the best solution will probably be this:
Lable lb2 = (Lable)PlaceHolder2.FindControl("lb2");
TextBox tb2 = (TextBox)PlaceHolder2.FindControl("tb2");
if(lb2 != null && tb2 != null)
{
SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
cmdinsert.ExecuteNonQuery();
}
If you have multiple lables and textboxes in the placeholder and you don't know how many pairs of them you have.
Then you probably need to make all of this in another way. I would recommand that you add a DataGrid to the placeholder and in the
DataGrid you add pairs of lable and textbox. Then the code would look something like this.
DataGrid dg = (DataGrid)PlaceHolder2.FindControl("dgTheDataGrid");
foreace(DataGridItem dgi in dg.Items)
{
Lable lb2 = (Lable)dgi.FindControl("lb2");
TextBox tb2 = (TextBox)dgi.FindControl("tb2");
if(lb2 != null && tb2 != null)
{
SqlCommand cmdinsertliftunion = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
cmdinsert.ExecuteNonQuery();
}
}
modified on Wednesday, October 8, 2008 9:20 AM
|
|
|
|
|
Thanks for the reply, i will try that and then get back to you.
For the record I didnt downvote your posts I have actually been responding to you and trying your solutions.
Just ignore J4amiec hes just on a power trip
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
thomasa wrote: Lable lb2 = (Lable)dgi.FindControl("lb2");
TextBox tb2 = (TextBox)dgi.FindControl("tb2");
But if I use this will I not need to know the ID of the label and textbox?
Thats the problem I have is trying to give them an ID as there could be a lot of them which are all created on the fly.
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
Yes, this would requier that you know the id.
That's way I suggested that you add a DataGrid where each row(DataGridItem) would contain 1 Label and 1 Textbox
|
|
|
|
|
Sorry, dident see the dgi.
That is acculy not a problem, for in each datagriditem every label would have the id="lb2".
as in:
DataGrid dg = new DataGrid();
dg.ID = "dgTheDataGrid";
dg.RunatServer = true;
DataGridItem dgi = new DataGridItem();
Lable lb2 = new Label();
lb2.ID = "lb2";
dgi.AddControl(lb2);
dg.AddControl(dgi);
PlaceHolder2.AddControl(dg);
The syntax in this code is probably wrong, but something like this.
|
|
|
|
|
Thanks for the reply again.
Im finding an error with the line DataGridItem dgi = new DataGridItem();
It says that: No overload for method 'DataGridItem' takes '0' arguments
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
Someone provided me with help on another forum so I have some code which works now:
Thanks for all the help though!
foreach (Control c in PlaceHolder2.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
int textboxId = PlaceHolder2.Controls.IndexOf(c);
if (textboxId > 0)
{
Control cLabel = PlaceHolder2.Controls[textboxId - 1];
try
{
Label lb2 = (Label)cLabel;
TextBox tb2 = (TextBox)c;
SqlCommand cmdinsert = new SqlCommand("INSERT INTO table(field1, field2) VALUES ('" + lb2.Text + "', '" + tb2.Text + "')", con);
cmdinsert.ExecuteNonQuery();
cmdinsertliftunion.ExecuteNonQuery();
}
catch (Exception)
{
}
}
}
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
Quite why this response got downvoted I dont know, but im voting it 4 as a (reasonably) good answer.
To .netman: this is not the first time ive seen good responses to your questions downvoted. Last time you said it wasnt you that downvoted, but this is too much of a coincidence! I suggest you are downvoting answers where you either dont like or dont understand the answer given. Therefore, for the time being at least I will personally not be answering any of your questions.
|
|
|
|
|
J4amieC wrote: Last time you said it wasnt you that downvoted, but this is too much of a coincidence!
Someone has downvoted some of my responses so I suggest you find out who that is and then you will find out who downvoted the other post.
J4amieC wrote: I suggest you are downvoting answers where you either dont like or dont understand the answer given.
You can suggest but you are wrong
J4amieC wrote: Therefore, for the time being at least I will personally not be answering any of your questions.
Considering your answer yesterday to one of my posts was a sarcastic 'go and read a beginners tutorial' and then you deleted it once I had quoted from it so frankly I couldnt care what you do
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
.netman wrote: Considering your answer yesterday to one of my posts was a sarcastic 'go and read a beginners tutorial'
Far from being sarcastic it was 100% serious. Your inital question on that day indicated you were unsure how to enumerate an arraylist of items. If that is giving you trouble you do indeed need a beginner's book (which is what i suggested). Everyone has to start somewhere.
|
|
|
|
|
An example tutorial for an arraylist closely applied to my situation was what I was looking for. Not a pure beginners book on asp.net which is what you suggested.
Oh well this is a pointless arguement as we clearly have totally different opinions on the siutation.
Deliver yesterday, code today, think tomorrow.
"http://www.heuse.com/cphumor.htm"
|
|
|
|
|
.netman wrote: An example tutorial for an arraylist closely applied to my situation was what I was looking for.
You seem to be having memory problems, here i'll quote your original question from yesterday:
"I have an arraylist which contains a number of items.
What is the best way to go through the items in the list.
ANy good tutorials on looping through the arraylist."
If that isn't screaming "beginning programming book" then its beyond me what is.
|
|
|
|