Pass password to excel file using vb script - sql-server

i have a excel file it is password enabled in a script task i am reading the file using Vb
I am not able to pass the password so that it allows me to read from the file.
How can i do it.Where can i pass the password. I also tried using FileInfo class there is no option for that.
present script is:
Dim fs As FileStream
Dim sr As StreamReader
Dim line As String
fs = File.OpenRead(Dts.Variables("File_Name").Value)
sr = File.OpenText(Dts.Variables("File_Name").Value)
'Read first line of the file
line = sr.ReadLine
Dim str As String
str = line
sr.Close()
fs.Close()
It works if the file is not password enabled.Any help. Please

You could use the actual Excel COM Object Model.
There is a property of the Workbook class that allows you to set the password.

Related

Data transfer using vb.net

I am new to vb.net and very confused at this point. I have experience with vba within excel and have taken a basic vb class at my university but I am clue less when it comes to this.
I have multi projects I would like to try, but I am stuck on how to transfer data, text string from one program to the other using a vb.net app!
I believe the process I am looking for is data binding? is that correct?
The 3 processes I am trying to accomplish.
Excel to Excel text or cell transfer/ copy and paste.
I have file A which is an excel file with rows and columns of data in cells. Then I have file B which is a template with graphs and some other formulas. I am trying to use a vb.net app to open file A copy all the data and paste into a sheet in file B. I have figured out how to open the files but as for the data transfer method I have not found anything.
Private Sub btn_Do_Click(sender As Object, e As EventArgs) Handles btn_Do.Click
Dim txtpath As String
Dim csvpath As String = "C:\Temp"
Dim FileTXT As String
Dim folderpath As String
Dim FinalFile As String
folderpath = "C:\Users\aholiday\Desktop\Data Dump"
FileTXT = cbo_FileList.Text
csvpath = "C:\Temp\" & FileTXT & ".csv"
txtpath = folderpath & "\" & FileTXT & ".txt"
FinalFile = "C:\Users\aholiday\Desktop\Book1"
File.Copy(txtpath, csvpath)
Process.Start("EXCEL.EXE", csvpath)
Process.Start("EXCEL.EXE", FinalFile)
File.Delete(csvpath)
End Sub
Access database to a chart in vb.net app.
Next I have an vb.net app that has a chart in it. How would I get data from an excel file into it. I have no code for this because I am not sure what the method I need is. I need this to be "live" meaning if someone changes something in the excel file it will update on the chart in the app. But the the first step is getting the data out of excel and into the chart. This is the only example I could find and its for Microsoft access as the data source. It didn't work.
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)Handles Button1.Click
Chart1.Series.Add("Numbers")
Dim Conn As OleDbConnection = New OleDbConnection
Dim Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
Dim dataFile = "C:\Users\aholiday\Desktop\Database11.accdb"
Conn.ConnectionString = Provider & dataFile
Conn.Open()
'Adds data to chart
Dim cmd As OleDbCommand = New OleDbCommand("Select [Month],[Number] FROM [Table1]", Conn)
Dim dr As OleDbDataReader = cmd.ExecuteReader
While dr.Read
Chart1.Series("Table1").Points.AddXY(dr("Month").ToString, dr("Number").ToString)
End While
dr.Close()
cmd.Dispose()
End Sub
End Class
Excel to vb.net pictures and text string transfer.
And last. I have an vb.net app that have textbox and picture boxes in the design layout. I have an excel file with strings of text and pictures within cells. I would like code to copy the text and pictures and place them in the textbox and Picturebox.
I would like to learn how one would accomplish any of those.Please post any helpful content from sources. Any suggestions and especially examples would be awesome. Please nothing from Microsoft unless it crystal clear. I have spent about a day researching and googled everything I could think of and have found very little. None that work when I try to use it.
Thank you

Deserializing byte to image from sql through memory stream

I have convert an image to be saved in SQL Server Database as Binary with column name as "img". I have PictureBox1 ready to show the image.
Now I want to import the binary data back into image, and I'm trying this code in VB.net for example:
Dim queries As String
queries = "SELECT * from StudentData where Std_fname='" & ComboBox1.Text & "'"
Dim com As New SqlCommand(queries, sqlconn)
sqlconn.Open()
Dim ds As New SqlDataAdapter(queries, sqlconn)
Dim dr As SqlDataReader
dr = com.ExecuteReader()
While dr.Read
Std_fnameTextBox.Text = dr("Std_fname")
Std_lnameTextBox.Text = dr("Std_lname")
AgeTextBox.Text = dr("age")
AddressTextBox.Text = dr("address")
StateTextBox.Text = dr("state")
CityTextBox.Text = dr("city")
CountryTextBox.Text = dr("country")
Ic_passportTextBox.Text = dr("ic_passport")
DobDateTimePicker.Text = dr("dob")
PictureBox1.Image = dr("img") 'Here is the problem. If I run it, it ask me to convert Binary to Image first.
End While
sqlconn.Close()
The problem is, I don't know how to convert binary to image in this situation. And yes, I've been googling for it, but can't seem to get the right answer.
dr("img") returns either DBNull.Value, or a byte[]. You can use the stream overload of the Bitmap constructor to load this. In C# (should be easy to translate to VB), you can do it like this:
var imageData = (byte[])dr["img"];
using (var ms = new MemoryStream(imageData))
{
var bmp = new Bitmap(ms);
// Work with bmp
}
As Mark correctly noted, you're supposed to keep the stream open for the whole life-time of the Bitmap - this is actually a bit trickier than it seems, because the Bitmap doesn't even keep a reference to the stream.
The easiest way to handle this is to clone the bitmap after you create it, to remove the dependency on the stream. Unless you can do whatever work you need to do within the using - unlikely if you want to display it in a PictureBox.
You can also use ImageConverter.ConvertFrom directly, but all it does is create the MemoryStream if used on raw byte[] data :)

ASP Classic Getting and Setting the Conn to another Conn object

I am using this way to connect and use the Conn object for connections
<!-- #include file="dataconnections.asp" -->
In one of my ASP pages, I have a VBScript Onclick event that needs me to execute a stored procedure and pass the variables in the textboxes.
Right now, the Connection String is hardcoded in the ASP File itself and is not using the Conn object from the include file. Is there any proper way to execute the SP without using a new Conn, or how can I pass the current Conn from the include file to the one being used in the VBScript Onclick event?
I don't know what your include dataconnections.asp contains (would have been nice if you had posted the source code in your question) but a stored procedure is best run using the ADODB.Command object which exposes the ActiveConnection property.
This allows you to either Set your ADODB.Connection object or pass your connection string and let the ADODB.Command instantitate a new ADODB.Connection using that connection string.
Here are examples of using the two options
Use existing ADODB.Connection
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
'Use existing object from include file to set connection.
Set .ActiveConnection = YourConnObject
End With
IMPORTANT: When using this method make sure the YourConnObject (ADODB.Connection) should have been opened using
'Make sure connection has been opened.
Call YourConnObject.Open()
before assigning to the ADODB.Command object.
Use connection string variable (probably defined in your include) to set the connection that will be exclusively used by the ADODB.Command object.
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
'Use connection string to instantiate a new connection to be used
'exclusively by the ADODB.Command object.
.ActiveConnection = YourConnectionString
End With
NOTE: Unlike option 1 calling
'Close ADODB.Command object
Call cmd.Close()
Will instantaneously destroy the ADODB.Connection (that was assigned while setting the ActiveConnection property).
Useful Links
Using Stored Procedure in Classical ASP .. execute and get results

WPF Vb.net Copyto not working?

I am simply tyring to fire an event that copies a program to the Startup folder. I do not understand where I am going wrong? I keep on getting exception message. The file that is being copies is NOT in use.
Try
Dim DesktopLink As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim StartupFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
Dim info As New FileInfo(StartupFolder)
info.CopyTo(DesktopLink + "\doessomething.bat")
Catch ex As Exception
MessageBox.Show("Error: Can not copy to startup folder")
End Try
Right now, you're creating a FileInfo from a folder, not a file.
This should likely be:
Dim info As New FileInfo(Path.Combine(StartupFolder, "doessomething.bat"))
info.CopyTo(Path.Combine(DesktopLink, "doessomething.bat"))
Or, even easier:
Dim source = Path.Combine(StartupFolder, "doessomething.bat")
Dim target = Path.Combine(DesktopLink, "doessomething.bat")
File.Copy(source, target)

How to populate listbox from excel using C#

I have an windows application. I browse for the file and select the excel file using OpenFileDialog control. The excel file contains email id's in column A. I want to populate the list-box with excel file column values. Office 2003 is installed on my machine. Can somebody Please help me out?
Thanks in Advance.
Refer: Reading Excel files from C#
To connect to an excel file you need the appropriate connection string:
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=<YourExcelPath>;
Extended Properties=\"Excel 12.0;HDR=YES;\"";
After use the OleDb classes to query the information from the file:
string selectCmd = "SELECT * FROM <SheetName>";
using(OleDbConnection excelConn = new OleDbConnection(connString))
{
excelConn.Open();
OleDbCommand command = new OleDbCommand(selectCmd, excelConn);
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable sheetInfo = new DataTable();
dataAdapter.Fill(sheetInfo);
//Do something with the data.
Bind your control with this datatable here
}
So you need to replace "YourExcelPath" with the path of your excel file..

Resources