Value assigned more than once in single assignment - c#

I made script task that's downloading and saving on disk two spreadsheets from Google Drive using file ID and prepared URL address.
This is main() from my C# code, there are no things outside of it:
public void Main()
{
string m_FileId = Dts.Variables["User::varFileId"].Value.ToString();
string m_RemoteUrl = "https://docs.google.com/spreadsheets/d/" + m_FileId + "/export?format=xlsx";
string m_FilePath = null;
WebClient client = new WebClient();
try
{
m_FilePath = Dts.Variables["User::varFilePath"].Value.ToString() + Dts.Variables["User::varFileName"].Value.ToString();
client.DownloadFile(new System.Uri(m_RemoteUrl), m_FilePath);
m_FilePath = "";
m_FileId = Dts.Variables["User::varFileId2"].Value.ToString();
m_RemoteUrl = "https://docs.google.com/spreadsheets/d/" + m_FileId + "/export?format=xlsx";
m_FilePath = Dts.Variables["User::varFilePath"].Value.ToString() + Dts.Variables["User::varFileName2"].Value.ToString();
client.DownloadFile(new System.Uri(m_RemoteUrl), m_FilePath);
}
catch(Exception e)
{
Dts.Events.FireError(0, "FileDownload", e.Message
+ "\r" + e.StackTrace
+ " \rUrl: " + m_RemoteUrl
+ " \rFilePath: " + m_FilePath
+ " \rPath: " + Dts.Variables["User::varFilePath"].Value.ToString()
+ " \rFileName2: " + Dts.Variables["User::varFileName2"].Value.ToString()
, string.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Problem occurs exactly on every second time I run this code and I don't know how to get rid of it. There's just exception in my script task. I'm printing all variables that are used in this code, and as you can see there's something wrong with m_FilePath, it's like multiplied despite of being printed just once.
[FileDownload] Error: An exception occurred during a WebClient request.
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at ST_84b63d1593dd449886eb2b32dff40b2d.ScriptMain.Main()
Url: https://docs.google.com/spreadsheets/d/----------/export?format=xlsx
FilePath: C:\Google Drive extract\ga_manual_cost_file.xlsxC:\Google Drive extract\ga_manual_cost_file.xlsx
Path: C:\Google Drive extract\ga_manual_cost_file.xlsx
FileName2: ga_manual_cost_file.xlsx
SSIS variables that I'm using are ReadOnly, and are used only in this script task(I tried running only this part of control flow), and their values are as follows:

Related

Console.WriteLine is blank

I have a strange problem. I wrote a .net 2.0 console application running on Windows Server 2008 R2 that processes a file that's passed in via args When I run it by hand and pass in a file, I don't get an output. Nothing. No errors or exception of any kind. Event log is clear. I would expect that I get some output from the exe. I also have a Console.WriteLine in my catch statement. But again, no output whatsoever. Any ideas why?
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
namespace RTDXCentralFileTransmit
{
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length < 1)
{
Console.WriteLine("File name is required");
logit("File name is required");
return;
}
string filename = args[0];
Console.WriteLine("Now doing " + filename);
logit("Now doing " + filename);
if (!File.Exists(filename))
{
Console.WriteLine("File " + filename + " does not exists. Exiting.");
logit("File " + filename + " does not exists. Exiting.");
File.WriteAllText("Error.txt", "File " + filename + " does not exists. Exiting.");
return;
}
string fileplain = File.ReadAllText(filename);
string file64 = EncodeTo64(fileplain);
if (string.IsNullOrEmpty(file64))
{
Console.WriteLine("File " + filename + " is empty. Exiting");
logit("File " + filename + " is empty. Exiting");
File.WriteAllText("Error.txt", "File " + filename + " is empty. Exiting");
return;
}
FileInfo fi = new FileInfo(filename);
com.xxxxxxxxxxxxxxxxxx.api.Dispatch dis = new com.xxxxxxxxxxxxxxxx.api.Dispatch();
com.xxxxxxxxxxxx.api.FunnelResult fr = dis.Funnel(file64, fi.Name);
if (fr.Success)
{
Console.WriteLine("Success! " + fr.Result);
logit("Success! Response from the server: " + fr.Result);
File.WriteAllText("Success.txt", fr.Result);
return;
} else
{
Console.WriteLine("Failed!! " + fr.Result);
logit("Failed!! " + fr.Result);
File.WriteAllText("Error.txt", "Transmission Failed! " + fr.Result);
}
logit("Exiting for " + filename);
Environment.Exit(0);
} catch (Exception e)
{
Console.WriteLine("Error. " + e.ToString());
return;
}
}
static void logit (string s)
{
File.AppendAllText("FileTransmit.log",DateTime.Now.ToString() + ":" + s + Environment.NewLine);
}
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes
= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue
= System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
}
}
I found it. The issue was between the chair and the computer.
The project that I cloned, even though it had tons of Console.WriteLine, it was actually was being built as Windows Application. That explains why we have no output for the Console.WriteLine.
Since it was being build as windows application, the console doesn't wait for the program to finish. It returns right away - which totally threw me off seeing that many console.writelines. However, the program is still running and, with a large file, it takes a while to post it to the remote server. After waiting a minute or two all of 10 MB file was transmitted successfully as it was indicated in the logs.

Updating front end during postback

I have a method that runs through a loop that can take quite a while to complete as it requires getting data back form an API.
What I would like to do is display a message on the front end explaining how the system is progressing during each loop. Is there a way to update the front end while processing?
public static void GetScreenshot(List<string> urlList, List<DesiredCapabilities> capabilities, String platform, Literal updateNote)
{
foreach (String url in urlList)
{
String siteName = new Uri(url).Host;
String dir = AppDomain.CurrentDomain.BaseDirectory+ "/Screenshots/" + siteName + "/" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
foreach (DesiredCapabilities cap in capabilities)
{
String saveDirectory = "";
if (platform == "btnGenDesktopScreens")
{
saveDirectory = dir + "/" + cap.GetCapability("os") + "-" + cap.GetCapability("os_version") + "-" + cap.GetCapability("browser") + cap.GetCapability("browser_version");
}
else if(platform == "btnMobile")
{
saveDirectory = dir + "/" + cap.GetCapability("platform") + "" + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
}
updateNote.Text += "<br/>" + cap.GetCapability("platform") + " - " + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
//I'd like to display a message here
TakeScreenshot(url, cap, saveDirectory);
//I'd like to display a message here
}
}
}
Has anyone come across a method of doing this?
Depending on how you're returning the feedback to the user, you might be able to do this by using HttpResponse.Flush in a loop to push parts of the HTML response to the user a bit at a time. See https://msdn.microsoft.com/en-us/library/system.web.httpresponse.flush(v=vs.100).aspx

Login and Register using System.IO

Don't Know What's wrong but I can't Access even It created a directory.. This is my code
private void btnRegister_Click(object sender, EventArgs e)
{
try
{
var sw = new System.IO.StreamWriter("E:Praisey\\" + txtAcc.Text + "\\Login.ID");
sw.Write(txtAcc.Text + "\n" + txtZipCode.Text);
sw.Close();
}
catch(System.IO.DirectoryNotFoundException ex)
{
System.IO.Directory.CreateDirectory("E:Praisey\\" + txtAcc.Text + "\\Login.ID");
var sw = new System.IO.StreamWriter("E:Praisey\\" + txtAcc.Text + "\\Login.ID");
sw.Write(txtAcc.Text + "\n" + txtZipCode.Text);
sw.Close();
}
}
it always getting error! The error is
UnauthorizedAccessException was unhandled
Access to the path 'E:\Praisey\48492995\Login.ID' is denied.
As I see from your catch block, "E:Praisey\\" + txtAcc.Text + "\\Login.ID" is actually a directory. You must also add the file name to the path.
var sw = new System.IO.StreamWriter("E:Praisey\\" + txtAcc.Text + "\\Login.ID\\filename.txt");
This error occurs (had it yesterday too) when the Directory does not exist. You need to create it before starting your StreamWriter.
if (!Directory.Exists("E:Praisey\\" + txtAcc.Text))
Directory.CreateDirectory("E:Praisey\\" + txtAcc.Text);

OrientDB Query works sometimes and at other times throws FormatException

I'm having a problem with this query below. I'm connecting to OrientDB using the C# driver and running the query in my c# code. All this code is in a loop and the queries in the try block works for a number of iterations and afterwards fails throwing a FormatException. At the point it fails I copy out the query and run it in the orientDB web interface and it returns the expect records so I'm sure it's not the input. So I'm so confused as to why it succeeds sometimes and fails at other times even when the input string is correct. Has anyone encountered a similar problem? Someone please help.
string origin = route[i].Trim();
string destination = route[i + 1].Trim();
List<ODocument> document = new List<ODocument>();
try
{
document = database.Query("select from (traverse out_Connects from " + origin + ") where in = " + destination);
//document = database.Query("select from Connects where (out = " + origin + " and in = " + destination + ")");
}
catch (Exception e)
{
//document = database.Query("select from Connects where (out = " + origin + " and in = " + destination + ")");
}

Output from an Exception class in App_Code

I am trying to see what exception is happening on my dev clients devserver (our servers are fine) and I have an TestException.cs class that handles the most of my exceptions from my sql statement class.
I have a method like so:
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < innerException.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + innerException.Errors[i].Message + "\n" +
"LineNumber: " + innerException.Errors[i].LineNumber + "\n" +
"Source: " + innerException.Errors[i].Source + "\n" +
"Procedure: " + innerException.Errors[i].Procedure + "\n");
}
//Console.WriteLine(errorMessages.ToString());
Response.Clear();
Response.Write("FAILURE");
Response.End();
But the Response.XX won't compile and if I use console.writeline when it hits it on the client server, the browser gets a connection lost webpage.
Is there a better way to do this?
You could try using the Trace class. If you link with a tracewriter in web.config pointing to a text file you can generate logging with Trace.WriteLine. Then you would just need the file uploaded to your network for reviewing.

Categories