Null reference error when adding picturebox dynamically to tabpage - c#

Getting the exception message:
Object Reference not set to an instance of an object.
My code is:
PictureBox myPicBox = new PictureBox();
myPicBox.Visible = true;
myPicBox.Image = Bitmap.FromFile(#"C:\Users\MyUser\Downloads\1341596825343s1jpg.jpg");
myPicBox.Dock = DockStyle.Fill;
this.MyTabPage.Controls.Add(myPicBox);
Where MyTabPage is the fourth page in a tab control. Any suggestions?
EDIT:
Stack Trace is Here
at ShipSiegeDashBoard.Form1.LoadSpaceStations() in
C:\ShipSiegeTool\ShipSiegeDashBoard\Form1.cs:line 46 at
ShipSiegeDashBoard.Form1..ctor() in
C:\ShipSiegeTool\ShipSiegeDashBoard\Form1.cs:line 31 at
ShipSiegeDashBoard.Program.Main() in
C:\ShipSiegeTool\ShipSiegeDashBoard\Program.cs:line 18 at
System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[]
args) at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx) at
System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state) at
System.Threading.ThreadHelper.ThreadStart()

I suppose your MyTabPage control isn't initialized yet. You should not try to use until after it has been initialized.

Related

Not able to download file - The remote server returned an error: (403) Forbidden

I'm trying to download a zip file in Windows Application , but is is throwing an error . My code is :
string url = "https://www.nseindia.com/content/historical/DERIVATIVES/2017/JUN/fo07JUN2017bhav.csv.zip";
using (WebClient wc = new WebClient())
{
wc.DownloadFile(url, #"c:\bhav\file.zip");
}
Exception details :
System.Net.WebException was unhandled HResult=-2146233079
Message=The remote server returned an error: (403) Forbidden.
Source=System StackTrace:
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at System.Net.WebClient.DownloadFile(String address, String fileName)
at unzip.Form1.downloadFile() in c:\users\ethicpro\documents\visual studio
2015\Projects\unzip\unzip\Form1.cs:line 30
at unzip.Form1..ctor() in c:\users\ethicpro\documents\visual studio 2015\Projects\unzip\unzip\Form1.cs:line 20
at unzip.Program.Main() in c:\users\ethicpro\documents\visual studio 2015\Projects\unzip\unzip\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart() InnerException:
Link is : https://www.nseindia.com/content/historical/DERIVATIVES/2017/JUN/fo07JUN2017bhav.csv.zip
I searched for other questions but didn't get the proper answer.
Try this
string url = "https://www.nseindia.com/content/historical/DERIVATIVES/2017/JUN/fo07JUN2017bhav.csv.zip";
string fileName = #"C:\Temp\tt.zip";
using (WebClient wc = new WebClient())
{
wc.Headers.Add("User-Agent: Other");
wc.DownloadFile(url, fileName);
}
The file might no longer exist.
I use VB.Net.
But you have to use the equivalent of on error resume next on this file or another file with a more recent date with and without on error resume next. Or the equivalent in C#.
Another option is that some characters cannot be downloaded, like : | > or .csv.zip should be .zip without the .csv.

How can I use rebus with simpleinjector

In my company we are using SimpleInjector as our IoC framework and are now looking at using Rebus as a wrapper for sending messages via RabbitMq. I am looking for help in creating a working example. I have tried the following code:
using Rebus.Activation;
using Rebus.Config;
using Rebus.Handlers;
using Rebus.Pipeline;
using Rebus.RabbitMq;
using Rebus.SimpleInjector;
using SimpleInjector;
using System;
using System.Threading.Tasks;
namespace SearchType.ProjectionA
{
class Program
{
static void Main(string[] args)
{
var container = new Container();
container.Register<IContainerAdapter, SimpleInjectorContainerAdapter>();
container.Register<IHandleMessages<string>, Handler>();
var adapter = container.GetInstance<IContainerAdapter>();
var bus = Configure.With(adapter)
.Logging(l => l.ColoredConsole())
.Transport(t => t.UseRabbitMq("amqp://localhost", "simpleinjector_consumer"))
.Start();
bus.Subscribe<string>().Wait();
Console.WriteLine("Projection A listening - press ENTER to quit");
Console.ReadLine();
}
}
public class Handler : IHandleMessages<string>
{
public Task Handle(string message)
{
return Task.Run(() =>
{
Console.WriteLine(string.Format("{0} - {1}", MessageContext.Current.Message.Headers["rbs2-corr-id"], message));
});
}
}
}
When i try and run this console application I am getting the following error:
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=The container can't be changed after the first call to GetInstance, GetAllInstances and Verify. The following stack trace describes the location where the container was locked:
at SearchType.ProjectionA.Program.Main(String[] args) in C:\HRG\TravTech\Springboard\SearchType\SearchType.ProjectionA\Program.cs:line 34
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Source=SimpleInjector
StackTrace:
at SimpleInjector.Container.ThrowWhenContainerIsLocked()
at SimpleInjector.Container.AddRegistration(Type serviceType, Registration registration)
at SimpleInjector.Container.RegisterSingleton[TService](TService instance)
at Rebus.SimpleInjector.SimpleInjectorContainerAdapter.SetBus(IBus bus)
at Rebus.Config.RebusConfigurer.Start()
at SearchType.ProjectionA.Program.Main(String[] args) in C:\HRG\TravTech\Springboard\SearchType\SearchType.ProjectionA\Program.cs:line 36
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Does anyone know what I can do to fix this? I don't want to have to declare all the dependencies myself.
Edit: thank you Steven for your reply. I have changed the code according to your answer and am now getting a different error.
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=The configuration is invalid. Creating the instance for type IMessageContext failed. The registered delegate for type IMessageContext threw an exception. Attempted to inject the current message context from MessageContext.Current, but it was null! Did you attempt to resolve IMessageContext from outside of a Rebus message handler?
Source=SimpleInjector
StackTrace:
at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
at SimpleInjector.Container.VerifyInstanceCreation(InstanceProducer[] producersToVerify)
at SimpleInjector.Container.VerifyThatAllRootObjectsCanBeCreated()
at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
at SimpleInjector.Container.Verify()
at SearchType.ProjectionA.Program.Main(String[] args) in C:\HRG\TravTech\Springboard\SearchType\SearchType.ProjectionA\Program.cs:line 27
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
HResult=-2146233088
Message=The registered delegate for type IMessageContext threw an exception. Attempted to inject the current message context from MessageContext.Current, but it was null! Did you attempt to resolve IMessageContext from outside of a Rebus message handler?
Source=SimpleInjector
StackTrace:
at SimpleInjector.InstanceProducer.GetInstance()
at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
InnerException:
HResult=-2146233079
Message=Attempted to inject the current message context from MessageContext.Current, but it was null! Did you attempt to resolve IMessageContext from outside of a Rebus message handler?
Source=Rebus.SimpleInjector
StackTrace:
at Rebus.SimpleInjector.SimpleInjectorContainerAdapter.<SetBus>b__7()
at lambda_method(Closure )
at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()
at SimpleInjector.InstanceProducer.GetInstance()
InnerException:
The error indicates that IMessageContext can only be instantiated inside a message handler. Is there a way to ignore certain errors?
I think the exception is clear; Simple Injector prevents registration after you already resolved. Reasons for doing this are described here.
The solution is to manually create the SimpleInjectorContainerAdapter and prevent relying on the container's auto-wiring capability for the adapter:
var container = new Container();
IContainerAdapter adapter = new SimpleInjectorContainerAdapter(container);
container.Register<IHandleMessages<string>, Handler>();
var bus = Configure.With(adapter)
.Logging(l => l.ColoredConsole())
.Transport(t => t.UseRabbitMq("amqp://localhost", "simpleinjector_consumer"))
.Start();
container.Verify();

How do I extract specific HTML's part of text using HtmlAgilityPack?

When viewing the page source for a page I use CTRL-F to find all occurrences of "id=", which gives me 82 results. What I want to do is to extract only the numbers after the "id=". For example, if the attribute is id=344 then I only want to get the 344 as string and add it to the List.
The way I'm doing it now I'm not getting links I thought I will get all the links this way and make filter after it but I'm getting empty string and some texts nothing from what I wanted. I guess doing InnerText is wrong.
Source View
idsnumbers = new List<string>();
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = hw.Load("http://www.tapuz.co.il/forums2008/");
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href]"))
{
idsnumbers.Add(link.InnerText);
}
Update getting null exception:
System.NullReferenceException was unhandled
_HResult=-2147467261
_message=Object reference not set to an instance of an object.
HResult=-2147467261
IsTransient=false
Message=Object reference not set to an instance of an object.
Source=WindowsFormsApplication1
StackTrace:
at WindowsFormsApplication1.Form1..ctor() in d:\C-Sharp\Tapuz Images\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 50
at WindowsFormsApplication1.Program.Main() in d:\C-Sharp\Tapuz Images\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
You should read ids from the attributes. InnerText is just for the text inside the tag, between the opening and closing brackets. So:
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href]"))
{
idsnumbers.Add(link.Attributes["id"].Value);
}
And if you want to further extract only numbers from ids, you could use RegEx or int.TryParse.

Unhandled Exception: System.ComponentModel.Win32Exception: The operation completed successfully

Whenever I try to run this code: (code shortened to keep the post short)
// Generate a random capital letter
char key = (char)(_random.Next(24) + 65);
// 50% make it lowercase
if (_random.Next(2) == 0)
{
key = Char.ToLower(key);
}
SendKeys.SendWait(key.ToString());
I get this error:
Unhandled Exception: System.ComponentModel.Win32Exception: The operation completed successfully
at System.Windows.Forms.SendKeys.SendInput(Byte[] oldKeyboardState, Queue previousEvents)
at System.Windows.Forms.SendKeys.Send(String keys, Control control, Boolean wait)
at System.Windows.Forms.SendKeys.SendWait(String keys)
at DrunkPC.Program.DrunkKeyboardThread() in c:\Users\SPC\Documents\Visual Studio 2013\Projects\DrunkPC\Program.cs:line 91
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Line 91 is:
SendKeys.SendWait(key.toString());

fluent migrator is not creating synonym

I am using Fluent Migrator 1.2.1
and i am getting exception while i am trying to create synonym
Object reference not set to an instance of an object.
StackTrace :
at FluentMigrator.Builders.Execute.ExecuteExpressionRoot.Sql(String sqlStatement)
at Tavisca.Catapult.Database.Rates.Migrations._004TagsAtProductLevel.Up() in d:\Projects\IMS\Git Source\tavisca-catapult-databases\Tavisca.Catapult.Database.Migrations\Tavisca.Catapult.Database.Rates\Migrations\004TagsAtProductLevel.cs:line 27
at Tavisca.Catapult.Database.Migrator.Program.Main(String[] args) in d:\Projects\IMS\Git Source\tavisca-catapult-databases\Tavisca.Catapult.Database.Migrations\Tavisca.Catapult.Database.Migrator\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
migration code is as follows
var contentDbConnectionString =
ConfigurationManager.ConnectionStrings["DBContent"].ConnectionString;
var ratesDbConnectionString =
ConfigurationManager.ConnectionStrings["DBRate"].ConnectionString;
var contentDbConnectionStringBuilder = new SqlConnectionStringBuilder(contentDbConnectionString);
var sqlConnectionBuilder = new SqlConnectionStringBuilder(ratesDbConnectionString);
var command = "CREATE SYNONYM [dbo].[Table1] FOR [" + sqlConnectionBuilder.DataSource + "].[" + contentDbConnectionStringBuilder.InitialCatalog + "].[dbo].[Table1];" +
"CREATE SYNONYM [dbo].[Table2] FOR [" + sqlConnectionBuilder.DataSource + "].["+ contentDbConnectionStringBuilder.InitialCatalog + "].[dbo].[Table2];";
Execute.Sql(command); // Exception will come here.
same script runs perfectly fine in query analyzer and synonym works too.

Categories