Using: C# and .net
I want to use a try catch to display an image of the product in a new window window. Everything works unless the image does not exist meaning a (HTTP Error 404 - File or directory not found) page. If that happens the button just simply does nothing when clicked.
What I want to happen is when you click the button and the file does not exist that the user is taken to a "Image does not exist" page. I have tried to do that in the following code. It does not work. Thanks for any advice!
bool ImageExists = true;
try
{
webResponse = webRequest.GetResponse();
}
catch
{
ImageExists = false;
}
if (ImageExists == true)
{
ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + PathToFolder + "');", true);
}
else
{
System.Diagnostics.Process.Start("http://www.companysite.com/noimage.jpg");
}
Edit: Changed to bool.
Try Catch won't trigger in this. See the link below
How to: Request Data Using the WebRequest Class
4.. You can access the properties of the WebResponse or cast the WebResponse to a protocol-specific instance to read protocol-specific
properties. For example, to access the HTTP-specific properties of
HttpWebResponse, cast the WebResponse to a HttpWebResponse reference.
The following code example shows how to display the status information
sent with a response.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
Add logic around the status return.
Related
I built a login/register system, and I want that when you create a username it checks if email exists, it works fine and it shows the message box "Email exists", but when it is a new user and there is no email that exists, it crashes.
Here is the exception message:
(System.NullReferenceException) Message=The object reference was not set to an object instance
Code:
FirebaseResponse response = await client.GetTaskAsync("Information/" + Emailtextbox.TextName);
Data result = response.ResultAs<Data>();
if (Emailtextbox.TextName == result.Email)
{
MessageBox.Show("Email exists");
} else
{
var data = new Data
{
Email = Emailtextbox.TextName,
Fullname = Fullnametextbox.TextName,
Password = EncryptSHA.GetShaData(PasswordTextbox.TextName)
}
};
Updating this based on the screenshot of the error as well as the information provided in the following comments.
It looks like your error has to do with what's being returned from your client.GetTaskAsync("Information/" + Emailtextbox.Textname); call.
My recommendation would be to try and understand what it is you're receiving from that call (what's stored in your response object). With the latest screenshot I see that the Body is null, and that might be part of the problem. Try expanding what you see in the Response object in your response and see if you're even receiving any kind of data you can use and go from there.
I want to check whether a facebook user liked my facebook page or not. I got so many solutions using javascript but I want to implement this requirement in ASP.Net.
I copied the code from the below link:
http://duanedawnrae.com/Blog/post/2012/02/29/Determine-if-a-Facebook-user-Likes-your-page-with-ASPNET.aspx
I got the below ASP.Net code which works for the same.
ASP.Net code:
public class WebService : System.Web.Services.WebService
{
[WebMethod()]
public string GetFacebookLikeStatus(string fbpageid, string fbappid, string fbtoken, string fburl)
{
string strReturn = null;
// Placeholder for the Facbook "like" API call
string strURL = null;
strURL = "https://graph.facebook.com/me/likes?access_token=" + fbtoken;
// Placeholder for the Facebook GET response
WebRequest objGETURL = null;
objGETURL = WebRequest.Create(strURL);
// Declare response stream
Stream objStream = null;
// Declare The Facebook response
string strLine = null;
// Declare a count on the search term
int intStr = 0;
try
{
// Create an instance of the StreamReader
StreamReader objReader = new StreamReader(objStream);
// Get the response from the Facebook API as a JSON string.
// If access_token is not correct for the logged
// on user Facebook returns (400) bad request error
objStream = objGETURL.GetResponse().GetResponseStream();
// If all is well
try
{
// Execute the StreamReader
strLine = objReader.ReadToEnd().ToString();
// Check if Facebook page Id exists or not
intStr = strLine.IndexOf(fbpageid); // if valid return a value
if (intStr > 0)
{
strReturn = "1";
// if not valid return a value
}
else
{
strReturn = "0";
}
objStream.Dispose();
}
catch (Exception ex)
{
// For testing comment out for production
strReturn = ex.ToString();
// Uncomment below for production
//strReturn = "Some friendly error message"
}
}
catch (Exception ex)
{
// For testing comment out for production
strReturn = ex.ToString();
// Uncomment below for production
//strReturn = "Some friendly error message"
}
return strReturn;
}
}
The above code contains a webservice which contains a single function. The function contains four input parameters and returns a single output string.
But when I run this webservice I got the error, “Value cannot be null. Parameter name: stream”. This error is coming because the “objStream” variable is set to null. Please fix the issue so that I can get my correct output as I dont know how to implement my requirement.
Like Gating is not allowed on Facebook, and neither is incentivizing users to like your Page. Users must like something only because they really want to, you can´t reward them in any way.
That being said, you would need the user_likes permission to use /me/likes, and you would need to get it approved by Facebook. Which will not happen just for checking if the user liked your Page.
Btw, that article is from 2012. A lot of stuff changed since then.
I created an asp.net site for downloading documents. I handle this with Page.Response.
try {
...
EndpointAddress endPoint = new EndpointAddress("xxxxx.svc");
FileServiceClient fileServiceProxy = new FileServiceClient(binding, endPoint);
// WCF WebService call
Stream stream = fileServiceProxy.GetFileStream(filePath);
Page.Response.ContentType = "application/pdf";
Page.Response.AddHeader("Content-Disposition",string.Format ("attachment; fileName=\"{0}\"", Path.GetFileName(filePath)));
Page.Response.AddHeader("Accept-Ranges", "bytes");
if (buffer != null){
Page.Response.BinaryWrite(buffer);
}
Page.Response.Flush();
}
catch (Exception e)
{
Page.Response.Clear();
Page.Response.ClearContent();
Page.Response.ClearHeaders();
}
finally
{
Page.Response.End();
}
And while the file is loading from a webservice I want to display a hourglass cursor. Showing loading cursor is working.
protected void Page_Load(object sender, EventArgs e)
{
btnDownload.Attributes.Add("onclick", "document.body.style.cursor = 'wait';");
}
But I can't change it back to normal cursor. I think because I don't fire a post back or don't reload the site.
What can I do to set default cursor if buttonClick event is over without site reload!?
Update: Updated the code with the wcf webservice call. I call the webservice with file path and get a stream back which I write to Page.Response.BinaryWriter
# Frédéric Hamidi THX for the link. I change my approach and display a jquery waiting dialog until file transfer is finished.
File download dialog
My Requirement: I want to know which page I am currently in so that if any test fails I want to pass the current page's URL to a method and get the home button link. Ultimately navigating to the home link in case of any exception.
Is there a way to achieve it ?
The URL should be in the address bar of the browser, just read it out of there.
One way of reading out the value is to record an assertion on the value in the address bar, then copy the part of the code in the recorded assertion method that accesses the value.
Another way is to use the cross-hairs tool to select the address area, then (click the double-chevron icon to open the left hand pane and) add the UI control for the selected area. Then access the value.
This will return the top Browser:
BrowserWindow bw = null;
try
{
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
var browser = new BrowserWindow() /*{ TechnologyName = "MSAA" }*/;
PropertyExpressionCollection f = new PropertyExpressionCollection();
f.Add("TechnologyName", "MSAA");
f.Add("ClassName", "IEFrame");
f.Add("ControlType", "Window");
browser.SearchProperties.AddRange(f);
UITestControlCollection coll = browser.FindMatchingControls();
// get top of browser stack
foreach (BrowserWindow win in coll)
{
bw = win;
break;
}
String url = bw.Uri.ToString(); //this is the value you want to save
}
catch (Exception e)
{
throw new Exception("Exception getting active (top) browser: - ------" + e.Message);
}
finally
{
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;
}
This is what I am trying to do.
Windows Form App loads with web browser
Goes to a certain link
Checks to see if the url bar is empty, if not it takes the current url and writes to a string and then navigates to some other url.
I keep getting Cannot implicitly convert type 'System.Uri' to 'string'
and I have tried a few things but can't get my head around it.
string url;
try
{
if (webBrowser1.Url != null)
{
// url = webBrowser1.Url;
MessageBox.Show("Success!");
}
else
{
MessageBox.Show(":(!");
}
}
catch
{
MessageBox.Show("Something Screwed Up");
}
Now at this point I get :( when I comment out the error. This is on form1.cs - should I be doing this on program.cs? It seems like the object may not be created a the point when I check but I have no idea. By default the form loads with a URL pre-loaded.
webBrowser1.Url is URI object
url is String object
it tells you those are diffrent types and there no implicit convertion
url = webBrowser1.Url.ToString();
see great article:
http://msdn.microsoft.com/en-us/library/ms173105.aspx