|
hi,
I am trying to autoformat an Excel (.xls) file and then import it into Sql server 2000 table using C# in Asp.net. But when i try to autoformat the excel file i get the exception "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Someone please help me to solve this problem
Here is the code.
sing System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Text;
using Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection Cn = new SqlConnection("server=143.14.45.137; database= NitDB; User ID=uy; Password= g");
SqlCommand Cm = new SqlCommand("Select * from ExcelFile", Cn);
Cn.Open();
SqlDataReader St = Cm.ExecuteReader();
gdExcelFile.DataSource = St;
gdExcelFile.DataBind();
St.Close();
Cn.Close();
}
public static void PrintProgressBar()
{
StringBuilder sb = new StringBuilder();
sb.Append("<div id='updiv' style='Font-weight:bold;font-size:11pt;Left:320px;COLOR:black;font-family:verdana;Position:absolute;Top:140px;Text-Align:center;'>");
sb.Append(" <script> var up_div=document.getElementById('updiv');up_div.innerText='';</script>");
sb.Append("<script language=javascript>");
sb.Append("var dts=0; var dtmax=10;");
sb.Append("function ShowWait(){var output;output='Reading data from excel...PLEASE WAIT!';dts++;if(dts>=dtmax)dts=1;");
sb.Append("for(var x=0;x < dts; x++){output+='';}up_div.innerText=output;up_div.style.color='red';}");
sb.Append("function StartShowWait(){up_div.style.visibility='visible';ShowWait();window.setInterval('ShowWait()',100);}");
sb.Append("StartShowWait();</script>");
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Flush();
}
public static void ClearProgressBar()
{
StringBuilder sbc = new StringBuilder();
sbc.Append("<script language='javascript'>");
sbc.Append("alert('Data exported successfully');");
sbc.Append("up_div.style.visibility='hidden';");
sbc.Append("history.go(-1)");
sbc.Append("</script>");
HttpContext.Current.Response.Write(sbc);
}
protected void btnExport_Click(object sender, EventArgs e)
{
PrintProgressBar();
{
OdbcConnection connection;
SqlBulkCopy bulkCopy;
string ConnectionString = @"server=178.17.44.137;database= NitDB; User ID= er; Password=d";
string connstr = @"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + fyle.Value;
using (connection = new OdbcConnection(connstr))
{
string[] temp;
string fn1;
fn1 = System.IO.Path.GetFileName(fyle.PostedFile.FileName); \\ fyle is the Html File upload control
temp = fn1.Split('.');
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excel = default(Microsoft.Office.Interop.Excel.Application);
Microsoft.Office.Interop.Excel.Workbook wb = default(Microsoft.Office.Interop.Excel.Workbook);
excel = new Microsoft.Office.Interop.Excel.Application();
// I get the exception at the below statement.
wb = excel.Workbooks.Open(fn1, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
excel.Visible = true;
//wb.Activate();
excel.Cells.Select();
excel.Columns.AutoFit();
excel.Rows.AutoFit();
OdbcCommand command = new OdbcCommand("Select * FROM [" + temp[0].ToString() + "$]", connection);
connection.Open();
using (OdbcDataReader dr = command.ExecuteReader())
{
using (bulkCopy = new SqlBulkCopy(ConnectionString))
{
bulkCopy.DestinationTableName = "ImportedExcel";
bulkCopy.WriteToServer(dr);
}
dr.Close();
}
}
bulkCopy.Close();
connection.Close();
}
ClearProgressBar();
}
}
Nitin Raj Bidkikar
|
|
|
|
|
Hi, All!
I need to develop hook for income/outgoing email messages using MAPI hooks. How can doing that? Some links or samples will be very good
|
|
|
|
|
Hello,
perhaps somebody can help me to find the reason for the crashes I have.
I have a MFC Application with MFC extension DLLs.
One of the dialogs is a window with a CPropertySheet.
One CPropertyPage is a host for ActiveX Controls providing extending UI.
This CPropertyPage has a CWnd member to host the ActiveX Controls dynamically created runtime using the CWnd::CreateControl method.
I have a VB ActiveX Control providing needed extending UI.
It can be loaded and used without problems.
The only problem occurs, when the application exits:
1. The CPropertySheet is destroyed ... the CPropertyPage is destroyed, ...
... which releases its previously queried interfaces,
... sends a WM_DESTROY to the CWnd hosting the ActiveX Control and
... deletes/frees the CWnd
2. Somewhere at the end just before invoking "exit" open "OLE" connections/loaded "OLE" libraries are terminated by Microsoft internal code. I saw that the ActiveX OCX module was active until this moment.
3. In the "exit" invocation the MFC application crashes from within the VB ActiveX Control. The VB ActiveX Control does not execute its UserControl_Terminate handler, so it seems, that there happens an irregular deletion of the VB ActiveX Control.
This crash happens currently only in the debug version of our application. But adding CommonControls to the VB ActiveX Control UI causes a crash even in the release version of this MFC application.
Using a MFC ActiveX Control providing some UI, too, no problems occur.
The only difference seen while releasing the last reference to the previously queried interface are that ...
... the release on the interface of VB ActiveX Control returns 4 still valid references,
... while the release on the interface of the MFC ActiveX Control returns only 3 still valid references.
Thus I suspect, that at application end there is still a last reference valid, not released, which causes a crash after the OCX is unloaded from Microsoft internal code cleaning up before the real application exit.
Something in the VB ActiveX Control wants still to work (like executing UserControl_Terminate), but the OCX is already unloaded.
But ... where and how can I find this last reference?
Debugging through the MFC code hosting the ActiveX Control didn't help really?
Has somebody here experienced a similar problem? Or can somebody help?
Thanks in advance,
Martin
|
|
|
|
|
Hi Martin,
I think you are on right track. Just make sure you are not holding any reference in your own code. Use smart pointer (CComPtr, CComQIPtr). If you queried for an interface, make sure you release it in your OnDestroy. I don't think it matters that you are using an MFC extension DLL, as long you release your interface, it should work fine.
Did you try creating a separate project (simple dialogbox) using the activeX in question?
In the end, we will remember not the words of our enemies, but the silence of our friends. - Martin Luther King Jr.
Ernest Laurentin
|
|
|
|
|
Hi Ernest,
thanks for your reply.
Until today morning I used no smart pointers, but using them does not change a thing.
I didn't expect a change in behaviour, because in my MFC C++ ActiveX control I could see, while debugging, that my reference handling was ok.
Ok - now I've introduced CComIQPtr and CComPtr, but it changes nothing.
The question is, why in comparison to the MFC ActiveX control one additional reference to the VB ActiveX control is hold!? And how/what is holding this reference to which interface?
The question raises, because releasing "my" interface I got a count of hold references of 3 with the MFC ActiveX control, but 4 with the VB ActiveX control.
And debugging through the deletion of my hosting CWnd I see only 3 releases on the interfaces of the MFC and the VB Active control.
So - where is the last reference on a interface to be found?
And ... do I have the possibility to change this behaviour?
Best regards,
Martin
|
|
|
|
|
Hello again,
first - I'm sorry, because I didn't provide any information about the development environment:
Microsoft Visual Studio 6.0 SP6
Microsoft Visual Basic 6.0 SP6
And ... no ... I can not upgrade - currently!
Second - I created a new most simple MFC application only loading the VB ActiveX control and raising an exception on exiting.
It has a CWnd* member on which CreateControl is called and which is deleted while deleting the dialog object.
So it shows the same behavior than the big MFC application I normally work on.
Has someone a hit or suggestion?
Best regards,
Martin
|
|
|
|
|
Hello,
after trying a bit I changed my VB ActiveX Control to execute the "UserControl_Terminate" things in the "UserControl_Hide" event handler.
Only the call "App.LogEvent ..." stayed in the "UserControl_Terminate" event handler.
And now everything seems to work fine.
Even a VB ActiveX Control with CommonControls (not even touched in the "UserControl_Terminate" event handler) works now.
So - I don't know why, but now everything works.
I found a solution, but no understanding for the problem.
Best regards,
Martin
|
|
|
|
|
Hi,
I am using a third party COM library, I am initializing one interface(I1) and all interfaces are initialize with I1. Am I need to call realese on every inreaface when I used any function of these intefaces?
|
|
|
|
|
It depends on what you mean by "all interfaces are initialize with I1".
Presumably this means that the interface I1 is queried for the other interfaces and the rules of COM says that when an interface is queried for, the reference count is set to 1 is the query was successful.
This means that you most likely have to release the interface when you're done with it.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Is the same true for smart interface pointer?
modified on Tuesday, October 21, 2008 4:51 AM
|
|
|
|
|
Smart pointers such as CComPtr will call Release() when they go out of scope. If you try to call AddRef() or Release() on a CComPtr, you'll get an error.
The CComPtr has the COM rules for reference counting built in, so you don't have to worry about that if you're using CComPtr . Even CComQIPtr works the same way.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Hi
I have a server which wil be fetching live Prices and i have to convert this server into a com+ service which wil be running on the server.. i to use the com + service in my aspx (i.e) webapplication . when ever the com + component raises a event i should be able to capture the event raised by the com+ service.. can we achieve this... Pls help me out
|
|
|
|
|
hi friend
Can any body tell me what is the advantages of .pfx file. and why we use it.
wasim khan
|
|
|
|
|
hI,
I am using the MSComm port for recieving data in c#.net while recieving data in parts from the OnComm event of mscomm i am not able to get the correct frame i.e. instead of 60bytes i am getting 80 bytes.
Can anybody tell the solution
|
|
|
|
|
tauras81 wrote: instead of 60bytes i am getting 80 bytes.
Well, you get 20 for free.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Firstly, if you're using .NET 2.0 or later you should be using the SerialPort class. The MsComm control is ancient and wasn't particularly good even with Visual Basic 6.0.
Secondly, serial communications are not framed - write boundaries are not preserved. You will get whatever is in the buffer when the read timeout expires, depending on how you've set up your serial port timeouts; this may be less than the number of bytes you requested.
"Multithreading is just one damn thing after, before, or simultaneous with another." - Andrei Alexandrescu
|
|
|
|
|
|
You must always match AddRef and Release calls. Do not rely on the return value of Release.
QueryInterface and CoCreateInstance add a reference implicitly, so they must always be matched with a call to Release.
"Multithreading is just one damn thing after, before, or simultaneous with another." - Andrei Alexandrescu
|
|
|
|
|
In VS6 C++, I have a windowless ATL control. To get a Device Context from the container, I belive I need to execute the GetDC() method of one of the base classes for the control, specifically IOleInPlaceObjectWindowless. I tried to do this:
CDC *pDC = IOleInPlaceObjectWindowless::GetDC();
But it doesn't work so I am at a loss as to how to execute a method on the IOleInPlaceObjectWindowless interface.
Any help would be greatly appreciated,
Thanks,
Doug
Doug Knudson
|
|
|
|
|
You can use methods exposed by interfaces ,only. Or you want to say smething else ?
|
|
|
|
|
Thanks for the reply. I didn't ask my question very well, but basically I am writing a windowless ATL component and need the Device Context from the Parent/Container so that I may draw in event handlers (for example, in MouseMove()). During my length time searching, all I could find was people saying that I needed to just execute the GetDC() method of the IOleInPlaceObjectWindowless interface like this:
HDC hDC = NULL;
HRESULT hr = IOleInPlaceObjectWindowless::GetDC(myRect,myFlags,&hDC);
The compiler interprets the above as trying to execute a static member function, which, of course, GetDC() isn't. If I just use:
GetDC();
I get the API GetDC() that expects there to be a m_hWnd, which there isn't (windowless control), so I am pretty lost as to how to do this.
Thanks,
Doug
Doug Knudson
|
|
|
|
|
There is no GetDC method on the IOleInPlaceObjectWindowless interface, nor any of its base interfaces.
You should generally be doing all your drawing in your implementation of IViewObject::Draw , i.e. in OnDraw .
If you need to do some drawing outside of this, the GetDC call is on IOleInPlaceSiteWindowless, which you have to ask the container for. I believe you should call InternalGetSite (which is inherited from CComControlBase ) to retrieve a pointer to this interface.
"Multithreading is just one damn thing after, before, or simultaneous with another." - Andrei Alexandrescu
|
|
|
|
|
Hi Mike,
Thanks! I will give that a try. I am doing the bulk of my drawing in OnDraw() but I need to drag a selection rectangle during the MouseDown, MouseMove, MouseUp events so I need to (or at least I think I need to ) have the DC outside of what is handed to me in OnDraw() ??
Thanks again,
Doug
Doug Knudson
|
|
|
|
|
I am trying to access a .net dll from excel via VBA. I am able to successfully do that on my machine.
But when I try to do the same on user's machine(which does not have VS 2003 but has .net Framework 1.1) The excel (as soon as I open the excel file) gives me an error "File or assembly name QueryData, or one of its dependencies, was not found".
I follow the following steps on the user's machine:
1. Register the .net dll using regasm tool and create a .tlb file
Regasm QueryData.dll /tlb:QueryData.tlb
2. Add this tlb file to the references of the excel.
3. Close the excel sheet and open it again (I have some code in the workbook_open event of excel).
I don't get any errors while generating the tlb file, but as soon as I open the excel I get the error mentioned above. I have also copied all the dlls from the bin folder of my dev machine to the folder (having QueryData.dll) on the user machine.
I have no clue why the excel file is not able to find the dll. Any pointers??
Thanks,
Neeraj
|
|
|
|
|
How to convert interface object to its class?
I have an interface object say IMarkSet. I passed to an COM function. There i need to access a function which takes CMarkedSet .
Thanks
Mohan
|
|
|
|
|