UPDATE: Added Default.aspx.cs if it helps
I'm new in ASP.Net and I'm having trouble making a website. I'm having an error saying Could not load type 'wsclient._Default'. What is my error?
<%# Page Language="C#"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="wsclient._Default" %>
Default.aspx.cs
using localhost;
namespace wsclient
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
lblmessage.Text = "First Loading Time: " +
DateTime.Now.ToLongTimeString();
else
lblmessage.Text = "PostBack at: " +
DateTime.Now.ToLongTimeString();
}
protected void btnservice_Click(object sender, EventArgs e)
{
StockService proxy = new StockService();
lblmessage.Text = String.Format("Current SATYAM Price:{0}",
proxy.GetPrice("SATYAM").ToString());
}
}
}
Default.aspx
Try modifying your code like this:
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="wsclient_Default" %>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class wsclient_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
lblmessage.Text = "First Loading Time: " +
DateTime.Now.ToLongTimeString();
else
lblmessage.Text = "PostBack at: " +
DateTime.Now.ToLongTimeString();
}
protected void btnservice_Click(object sender, EventArgs e)
{
StockService proxy = new StockService();
lblmessage.Text = String.Format("Current SATYAM Price:{0}",
proxy.GetPrice("SATYAM").ToString());
}
}
Simply change "CodeBehind" to "CodeFile" in your Defualt.aspx page "Line 1"
Related
I need to convert a pdf file into jpeg using c#.
The solution (dll library) have to be free.
I have searched a lot of information on the web but seems that the library pdfiumviewer might be helpful here. It is also available as nuget.
I have tried this code without success, because the new file in jpg format is not saved.
How to do resolve this?
using PdfiumViewer;
using System;
using System.Drawing.Imaging;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
try
{
using (var document = PdfDocument.Load(#"sample.pdf"))
{
var image = document.Render(0, 300, 300, true);
image.Save(#"output.png", ImageFormat.Png);
}
}
catch (Exception ex)
{
// handle exception here;
}
}
}
}
Edit #01
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
try
{
using (var document = PdfDocument.Load(#"C:\\Users\\aaa\\Documents\\Visual Studio 2013\\WebSites\\GroupDocsConversion\\sample.pdf"))
{
var image = document.Render(0, 300, 300, true);
image.Save(#"C:\\Users\\aaa\\Documents\\Visual Studio 2013\\WebSites\\GroupDocsConversion\\output.png", ImageFormat.Png);
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}
}
Try this combined solution with iTextSharp and Ghostscript.NET ( available on Nuget ).
If necessary install Ghostscript 9.52 for Windows (32 bit)
I hope I was helpful.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="flupload" runat="server" />
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
using Ghostscript.NET;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
String PdfFolderPath = HostingEnvironment.MapPath("~/PdfFolder/" + flupload.FileName);
HttpPostedFile file = HttpContext.Current.Request.Files[0];
file.SaveAs(PdfFolderPath);
Pdf2ImageConversion(flupload.FileName, PdfFolderPath);
}
private void Pdf2ImageConversion(string FileName, string PdfFolderPath)
{
String FileNameWithoutExtension = Path.GetFileNameWithoutExtension(FileName);
String ImgFolderPath = HostingEnvironment.MapPath("~/ImgFolder/"
+ FileNameWithoutExtension + ".jpeg");
var info = new System.IO.FileInfo(ImgFolderPath);
if (info.Exists.Equals(false))
{
GhostscriptPngDevice img = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png16m);
img.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
img.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
img.ResolutionXY = new GhostscriptImageDeviceResolution(200, 200);
img.InputFiles.Add(PdfFolderPath);
img.Pdf.FirstPage = 1;
img.Pdf.LastPage = 1;
img.PostScript = string.Empty;
img.OutputPath = ImgFolderPath;
img.Process();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
I copied some code out of an old program and into a new one, it WAS working but now ALL of my objects have this error:
The name 'XXX' does not exist in the current context
I'm sure it has something to do with the behind the code file losing a link with the html but all the filenames and references I see match up. Is there something in particular I should check? Thanks in advance!
from html file
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="garrettPenfieldUnit4.Default" %>
from .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace garrettPenfieldUnit4
{
public partial class Default : System.Web.UI.Page
{
//Load events, automatically inserts todays date and selects the single king radio button
protected void Page_Load(object sender, EventArgs e)
{
arrivalDateBox.Text = DateTime.Today.ToShortDateString();
singleKingButton.Checked = true;
}
//When submit button is clicked show the two confirmation labels.
protected void submitButton_Click(object sender, EventArgs e)
{
submitLabel1.Visible = true;
submitLabel2.Visible = true;
}
//When the clear button is clicked clear the form and reset fields to their default values
protected void clearButton_Click(object sender, EventArgs e)
{
arrivalDateBox.Text = DateTime.Today.ToShortDateString();
departureDateBox.Text = String.Empty;
peopleDropDown.SelectedIndex = 0;
singleKingButton.Checked = true;
twoQueensButton.Checked = false;
singleQueenButton.Checked = false;
specialRequestsBox.Text = String.Empty;
firstNameBox.Text = String.Empty;
lastNameBox.Text = String.Empty;
addressBox.Text = String.Empty;
telephoneNumberBox.Text = String.Empty;
contactDropDown.SelectedIndex = 0;
}
}
}
i have an aspx, aspx.cs file, in the aspx file i m using javascript functions,
in the javascript code i use the getter (<%=getSize%>) method defined in the aspx.cs file,
but the page returns: "The Controls collection cannot be modified because the control contains code blocks" why?
<!-- language-all:lang-js -->
<%# Page Language="C#" AutoEventWireup="true" CodeFile="uploadFile.aspx.cs"
Inherits="UploadControl_CustomProgressPanel" Title="Untitled Page" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
var size = <%=getSize%>;
var id= 0;
function ProgressBar()
{
if(document.getElementById("FileUpload1").value != "")
{
document.getElementById("divProgress").style.display = "block";
document.getElementById("divUpload").style.display = "block";
id = setInterval("progress()",20);
return true;
}
else
{
alert("Select a file to upload");
return false;
}
}
function progress()
{
//size = size + 1;
if(size > 299)
{
clearTimeout(id);
}
document.getElementById("divProgress").style.width = size + "pt";
document.getElementById("lblPercentage").firstChild.data = parseInt(size / 3) + "%";
}
aspx.cs file:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class UploadControl_CustomProgressPanel : System.Web.UI.Page
{
protected int size = 2;
protected int getSize{ get { return this.size; }}
protected void Page_Load(object sender, EventArgs e)
{
string UpPath;
UpPath = "C:\\";
if (! Directory.Exists(UpPath))
{
Directory.CreateDirectory("C:\\");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//StatusLabel.Text = "File scelto, Upload in corso..";
if(FileUpload1.HasFile)
{
try
{
string filePath = Request.PhysicalApplicationPath;
filePath += "classic/hub/csv/";
filePath += FileUpload1.FileName;
this.size = 50;
FileUpload1.SaveAs(filePath);
//StatusLabel.Text = "Upload status: File uploaded!";
Label1.Text = "Upload successfull!";
}
catch(Exception ex)
{
//StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
You haven't posted the full HTML but try to move your script out of the <head> tag.
Is getSize a property or a function? If its a property size will take the value that it is at page load, if the its a function then you need to look at page methods as you cannot call it in this way, heres an example
I want my code to refresh the page once its shown the success label for 3 seconds.
How can i do this in c# code?
i have this following:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;
public partial class CAPTCHA_Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//ImageVerification
if (!IsPostBack)
{
SetVerificationText();
}
imCaptcha.ImageUrl = "captcha.ashx?d=" + DateTime.Now.Ticks;
}
public void SetVerificationText()
{
Random ran = new Random();
int no = ran.Next();
Session["Captcha"] = no.ToString();
}
protected void CAPTCHAValidate(object source, ServerValidateEventArgs args)
{
if (Session["Captcha"] != null)
{
if (txtVerify.Text != Session["Captcha"].ToString())
{
SetVerificationText();
args.IsValid = false;
return;
}
}
else
{
SetVerificationText();
args.IsValid = false;
return;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
SetVerificationText();
//Save the content
MailMessage mail = new MailMessage();
mail.From = new MailAddress(EmailTB.Text);
mail.To.Add("name#company.co.uk");
mail.Bcc.Add("test#test.co.uk");
mail.Subject = "Web Quote";
mail.IsBodyHtml = true;
mail.Body = "First Name: " + FNameTB.Text + "<br />";
mail.Body += "Email: " + EmailTB.Text + "<br />";
mail.Body += "Telephone: " + TelephoneTB.Text + "<br />";
mail.Body += "Query: " + QueryDD.Text + "<br />";
mail.Body += "Comments: " + CommentsTB.Text + "<br />";
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Send(mail);
sucessPH.Visible = true;
}
protected void Reset(object s, EventArgs e)
{
FNameTB.Text = "";
QueryDD.Text = "";
EmailTB.Text = "";
TelephoneTB.Text = "";
CommentsTB.Text = "";
txtVerify.Text = "";
}
}
So after sucessPH.Visible = true; i need to count 3 seconds and then refresh the page. This will then clear the form data and the user will also get 3 seconds to see the message to say the message was successfull.
Any ideas?
Since this is a client-side concern and not a server-side concern, you'll want to do this with JavaScript. Keep in mind that by the time the page is sent to the client all of the server-side code has completed and disposed. Trying to do this with server-side code will result in a lot of unnecessary complexity in this case.
In JavaScript you can reload the page with a delay with something like this:
setTimeout("window.location.reload()", 3000);
Well, if you're using webforms, you could use an update panel, and then use the timer component, just like this guy've done
Tutorial: How to refresh an UpdatePanel control at a timed interval
Right after sucessPH.Visible = true; add following line: ClientScript.RegisterClientScriptBlock(this.GetType(), "refresh", "setTimeout('window.location.href=window.location.href', 3000);", true);
Well.. you are in a web..
So your server can count 3, but you need the client to count 3 and then postback it to the server...
If your server only counts, then the user will not view anything..
Just make your application ajax enable, and use a timer ;)
This is the code for the page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="3000" ontick="Timer1_Tick">
</asp:Timer>
</div>
</form>
</body>
</html>
and in the code behind you have this:
protected void Timer1_Tick(object sender, EventArgs e)
{
//do your thing when reach this tick
}
This page will refresh exactly every 3 seconds.
This is .net 4.
We use email address instead of user id. But when the user enters his email address into the password recovery form and submits it, the site returns "We were unable to access your information. Please try again." and replaces the email value in the text box with a long string of characters and numbers (e.g. e61686cb-93a5-4737-8c40-52g8eb01bb67).
Here's the relevant aspx page code...
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<div class="login_page">
<div class="login_header">Password Reset</div>
<div class="login_body">
<asp:PasswordRecovery ID="PasswordRecovery1" runat="server"
onverifyinguser="PasswordRecovery1_VerifyingUser"
onsendingmail="PasswordRecovery1_SendingMail">
</asp:PasswordRecovery>
</div>
</div>
</asp:Content>
And the code behind...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Configuration;
using System.Web.Profile;
using System.Text;
namespace Sample.Web
{
public partial class PasswordReset : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, #"^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
protected void PasswordRecovery1_VerifyingUser(object sender, LoginCancelEventArgs e)
{
if (IsValidEmail(PasswordRecovery1.UserName))
{
string username = Membership.GetUserNameByEmail(PasswordRecovery1.UserName);
if (username != null)
{
PasswordRecovery1.UserName = username;
}
else
{
PasswordRecovery1.UserNameInstructionText = "We were unable to access your information. Check your user name and try again.";
e.Cancel = true;
}
}
else
{
PasswordRecovery1.UserNameInstructionText = "You must enter a valid e-mail address.";
e.Cancel = true;
}
}
protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
string pwd = Membership.GetUser(PasswordRecovery1.UserName).ResetPassword(PasswordRecovery1.Answer);
string email = StorageByMail.BLL.SBMUser.SelectUser(
StorageByMail.Data.User.GetIDFromUserName(PasswordRecovery1.UserName)).Email;
MailMessage m = new MailMessage(ConfigurationManager.AppSettings["AdminEmail"].Trim(), email);
m.ReplyTo = new MailAddress(ConfigurationSettings.AppSettings["AdminEmailReply"].Trim());
StringBuilder sb = new StringBuilder();
sb.Append("Please return to the site and log in using the following information.\n");
sb.Append("User Name: " + email + "\n");
sb.Append("Password: " + pwd);
m.Body = sb.ToString();
m.Subject = "Password reset from StorageByMail.com";
SmtpClient o = new SmtpClient(ConfigurationManager.AppSettings["SMTPHost"].Trim());
string smtpUser = ConfigurationSettings.AppSettings["SMTPUser"].Trim();
string smtpPass = ConfigurationSettings.AppSettings["SMTPPassword"].Trim();
if (smtpUser.Length > 0 && smtpPass.Length > 0)
{
o.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPass);
}
o.Send(m);
e.Cancel = true;
}
}
}
Without knowing a lot about ASP.NET membership, my guess would be that the line:
PasswordRecovery1.UserName = username;
// where username = Membership.GetUserNameByEmail(PasswordRecovery1.UserName);
is causing your problem. Even though you say you're not using username in your model, I'd bet that ASP.NET populates the username column in your database with a GUID. Take a look at your database, and see if that GUID matches up with the username column in your table for this user.