How to link a windows form in one project to a button in another project using MVC3 - c#

In my program I have a registration page where a user enters all his details to register to the system. On this page I have a button that when selected should open up a windows form that is located on another project, however when I select this button nothing is happening.
The code behind the windows form application:
public GenerateToken()
{
InitializeComponent();
}
public void Main()
{
Application.Run(GenerateToken.ActiveForm);
}
The above code is found in a project on its own. In another project this is the code for the controller that calls this windows form:
public ActionResult generateToken()
{
new TokenGenerator.GenerateToken().Main();
return RedirectToAction("RegisterPage");
}
The code in the view where the button is located for this button is:
<input id="GenToken" type="button" value="generateToken" onclick = "generateToken" />
Is there something that I am doing wrong regarding the button or is the way that I am trying to link them together wrong?

As you insist on this way, you have to create a communication channel between these two apps. You can use modern technologies for this purpose such as Named Pipes, .NET remoting or WCF hosted on IIS (better one).
If you choose Named Pipes or .NET remoting, you have to create windows service project and use it as server application. As shown in this sample, create a host object and register a channel in your windows service solution. You have to define a method in this hosted class as a runner of your windows application. Be noticed that you have to run it as process not by the way you did in your code. Your web app must call that method from server hosted object which will cause your windows app to run. How to start process is explained here. Let me know if you understand what to do.

I managed to solve this by creating a class in my windows application project and linking this class to my main mvc project. The class contained the following method:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SomeForm form1 = new SomeForm();
Application.Run(form1);
By linking this to a button in my main MVC project I was able to load the windows form on button click.

Related

Logging in to a website with C#

I'm sorry if this subject has already been answered, but I couldn't find what I needed (yet).
I'm working on a program that downloads files from university websites that use the same infrastructure. It's an open source project which I'm trying to support in my free time
(hosted in goodle code: http://code.google.com/p/highlearner/)
Until now we used GET and POST requests to login into the right page and download stuff. But the universities keep changing their websites and every little change requires teaking in Highlearner, which requires a new version, auto-updating all users, etc. Also, every university has its own login page, requiring me to tailor a login sequences..
So I'm looking for a more robust solution. Instead of manually redirecting and setting the HTTP parameters. Is there some kind of mini browser that supports with HTML + Javascript? No GUI is needed, I just need the engine.
This way, I will simply need to fill out the form parameters and let the browser do the work.
Thanks,
Nitay
You could try to automate the process with WatiN library . It allows you to click buttons, submit forms, etc.
using (var ie = new IE(loginUrl))
{
if (ie.TextField("username").Exists
&& ie.TextField("password").Exists)
{
ie.TextField("username").Value = "username";
ie.TextField("password").Value = "password";
ie.Button(Find.ByName("submit")).Click();
}
}

Working with domain services

I created a domain service on the server side. Build the project. But I still don't have the domain proxy at the client side for the created service. Should I add any new namespaces to the client project? I tried Add Service on the client project, but there is no newly created domain service in the list (I pressed Discover button).
Peter. If I understand your question exactly, these can be the right answer:
After clicking "Show All Files" button in your Solution Explorer, You can find the client proxy of domain services at the hidden Generated_Code folder.
The *.Web.g.cs code may be generated by reflection of server side compiled assembly.
So,you can use the proxy by just adding namespace after building solution.
For example,
using myApp.Web.Models;
using myApp.Web.Services;
using System.ServiceModel.DomainServices.Client;
namespace myApp
{
public partial class MainPage : UserControl
{
MyModelContext _context = new MyModelContext();
}
...
}

Pass value of textBox of WEB application to Desktop application's textBox using C#.NET

Everyone I am using C#.NET for the task. Let me explain briefly what i need actually. I have a WEB application in ASP.NET C# and the value/data inserted by the user in the 'TextBox'(Employee_name:) needs to be passed to desktop application on the same corresponding 'TextBox' say "Employee_name:" So how do i make it. Thanks.
Web applications can not initiate a communication. So you can't do this the way you'd (probably like).
You can have your desktop application call a method in you ASP.NET application and get back the value and then put it in that required textbox.
However, that would mean a two step process Since you'd have to know when to ask the GUI application to go get the data from the ASP.NET application and depending on your situation, you'll probably have to pass additional information to the ASP.NET application so it can provide the value from the correct "record" as well.
EDIT
Simple way to achieve this, using just regular Http request (no web services or WCF)
The Mark up in an .aspx file should have no html. Just the page directive like so:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="HttpTaskServer.WebForm1" %>
In your code behind file (.cs file of the page) you could do something like this:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.HttpMethod == "GET")
{
//Some parameter passed in by the client via QueryString (url)
var id = Request.QueryString["id"];
string data = GetTheData(id);
Response.Write(data);
}
}
}
For an example of how to make an http call from a GUI/Console application. Take a look at Code Listing 9 (the last code listing in the blog post). the link below will take you straight to code listing 9
http://www.matlus.com/httpwebrequest-asynchronous-programming/#codelisting9
I suggest:
- A webservice (WCF?) running on the IIS
- A webservice consumer in the desktop
The desktop client polls the web server if he has something for him on a regular interval. That or use WCF to set up duplex communication. That way the server can inform his clients the textbox has been changed. The communication still needs to be initiated by the desktop client.
I assume the scenario you are referring to here is a Front end web application and backoffice windows application. I am not sure why would need the data to be passed in real time but my suggestion would be to get your operation from Web application completed/stored in Database and then have that data pulled from DB to your windows application.

why does my silverlight reference to my wcf service blow up when I add a method to the wcf service that returns a generic list

I have built a WCF Service that is being consumed by a Silverlight app. At first I created one method that was very simple:
public String SfTest()
{
return "SF Test";
}
No poblem. My silverlight app references my service and displays "SF Test" in a textbox.
Now I add a method to my wcf service like this:
public List<String> GetTest()
{
List<String> list = new List<string>();
String a = "a";
list.Add(a);
String b = "b";
list.Add(b);
return list;
}
I update the reference to the service in my Silverlight app and the using statement in my xaml cs page throws an error like the service doesn't even exist although it is there.
I am assuming the problem has to do with datatypes or serialization or something like that but it is driving me up the wall. Why can't I consume a simple generic list in my Silverlight app through the WCF service.
Look at the Reference.cs file generated by the Add Service Reference... dialog box, and see if the appropriate proxy classes/etc. are being generated there.
I've run into issues like this before, where the Add Service Reference... dialog box isn't able to create the proxies for one reason or another. Unfortunately, it doesn't give you any feedback on why it can't generate the appropriate classes. To troubleshoot it better, my recommendation is to drop back to the SLSvcUtil.exe file (usually located in c:\Program Files\Microsoft SDKs\Silverlight\v4.0\Tools), and try to generate your proxies that way. It will quite possibly fail, but it will at least tell you why it's failing.

How to access Silverlight client and server side?

I have a real basic Silverlight app consisting solely of a label within an ASP.NET web page. It is included as an object.
If I want to change the Silverlight's label content via a button placed on the ASP.NET page. Could anyone provide a sample on how to do this both via javascript and server side via c#?
Thanks.
Silverlight is a client-side technology, so you can't access it directly from server.
However, you can use its scripting capabilities to achieve what you want. For instance, you ASP.NET button could write some javascript which will interact with you SL app.
// Silverlight code
[ScriptableMember]
public void Start()
{
// do something
}
// Javascript code
function start() {
if (confirm("Are you sure?")) {
document.getElementById("<%= SilverlightUpload.ClientID %>")
.content.myControl.Start();
}
}
Silverlight and JavaScript Interop Basics

Categories