CS0103 the name 'txtname' does not exist in the current context - c#

i get 'txtname','txthours','txtrate', and 'textgrosspay' does not exist in the current context. error code is CS0103. i have matched the words from the text boxes i have on designer with these words as well. any help is appreciated.
This image here shows the design part of my web form:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Grosspayapp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Compute_Click(object sender, EventArgs e)
{
String name;
String shours, srate;
int hours;
double rate, grosspay;
name = txtname.Text;
shours = txthours.Text;
hours = Convert.ToInt32(shours);
srate = txtrate.Text;
rate = Convert.ToDouble(srate);
grosspay = hours * rate;
txtgrosspay.Text = grosspay.ToString();
}
}
the source code is
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Grosspayapp.aspx.cs" Inherits="GrossPayApp3_20.GrossPayProgram" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
font-size: xx-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="auto-style1"> Gross Pay Application </span>
<br />
</div>
<p>
</p>
<p>
</p>
<p>
Name
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</p>
<p>
Hours
<asp:TextBox ID="txthours" runat="server"></asp:TextBox>
</p>
<p>
Rate
<asp:TextBox ID="txtrate" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="cmdcompute" runat="server" OnClick="Compute_Click" Text="Compute Gross Pay" />
</p>
<p>
Gross Pay
<asp:TextBox ID="txtgrosspay" runat="server"></asp:TextBox>
</p>
</form>
</body>
</html>

The attribute Inherits of the <%# Page directive should reference the page's class name Grosspayapp, but you have
Inherits="GrossPayApp3_20.GrossPayProgram"

Related

CS0103: The name does not exist in the current context

Here is the main form where I have to take user inputs. I have used Design option to create the form in visual studio.
WebForm1.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ArthmeticClient.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Enter Value 1:
<input id="Text1" type="text" /><br />
<br />
Enter Value 2:
<input id="Text2" type="text" /><br />
<br />
Resul:
<input id="Text3" type="text" /><br />
<br />
<input id="Button1" type="button" value="Addition" />
<input id="Button2" type="button" value="Multiplication" /><br />
<br />
</div>
</form>
</body>
</html>
I have used the same id for the input tag but still it cannot find the content.
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ArthmeticClient
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.IService1 ob = new ServiceReference1.Service1Client();
int num1 = int.Parse(Text1.Text);
int num2 = int.Parse(Text2.Text);
Text3.Text = Convert.ToString(ob.Sum(num1, num2));
}
protected void Button2_Click(object sender, EventArgs e)
{
ServiceReference1.IService1 ob = new ServiceReference1.Service1Client();
int num1 = int.Parse(Text1.Text);
int num2 = int.Parse(Text2.Text);
Text3.Text = Convert.ToString(ob.Multiply(num1, num2));
}
}
}
I have to just a small option, I have created simple form, I dont think there a problem with the id I have given. but it is still showing this error. Idk what is wrong.
Can anybody help...

html master page with data base sql server

plss help i have this problem:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
this is the master page code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link rel="shortcut icon" type="icon/x-icon" href="Main Images/favicon.ico" />
<link rel="stylesheet" type="text/css" href="MyStyle.css" />
<link href="style.css" rel="stylesheet" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body style="background-image: url('Main Images/background.jpg');">
<form id="form1" runat="server">
<div dir="rtl" style="background-color:#242020">
<audio src="songs/Jurassic Park Theme Song - Jurassic Park Theme Song.mp3" autoplay="" controls=""></audio><br /><br />
</div>
<div class="top">
<center> <img src="Main Images/logo.png" /></center>
<div>
<form name="login">
<span style="margin-top:25px;color:goldenrod;margin-left:3%;">Username<span style="margin-left:7px"><asp:TextBox type="text" id="userid" name="userid" style="color:fuchsia" placeholder="User Name" class="gg" runat="server"></asp:TextBox></span></span><br /><br />
<span style="margin-top:25px;color:goldenrod;margin-left:3%">Password<span style="margin-left:7px"><asp:TextBox class="gg" type="password" id="pswrd" name="pswrd" style="color:goldenrod" placeholder="Password" runat="server"></asp:TextBox><br /></span></span><p runat="server" id="ans" style="color:red"></p>
<span style="margin-left:4%"><asp:Button ID="Button1" onclick="Check_Click" class="login" runat="server" Text="Login" />
<input type="reset" class="cencel" value="Cancel"/></span><br /><br /><span style="margin-left:4.7%"><a class="button" href="Had Sign Up.aspx">sign up to HaD</a></span>
</form>
<script type="text/javascript">
function check(form) {
if(form.userid.value == "Oz Cohen" && form.pswrd.value == "guzguz8") {
window.open('HaDMan.aspx');
}
else {
if (form.userid.value != "Oz Cohen" && form.pswrd.value!= "guzguz8") { document.getElementById("ans").innerHTML = ("Eror Username and Password") }
if (form.userid.value == "Oz Cohen" && form.pswrd.value != "guzguz8") { document.getElementById("ans").innerHTML = ("Eror Password") }
if (form.userid.value != "Oz Cohen" && form.pswrd.value == "guzguz8") { document.getElementById("ans").innerHTML = ("Eror Username") }
}
}
</script>
</div>
<center>
<asp:Table ID="Table2" runat="server" style="margin-top:7px">
<asp:TableRow>
<asp:TableCell> <nav class="menu">
<ul class="clearfix">
<li>
<img style="margin-top:-20px"width="60" src="Main Images/lines.png" />
<ul class="sub-menu">
<li>My site page</li>
<li><a title="Got lost? Click here for the site map..." href="sitemap.aspx">Map of the site</a></li>
<li>Dinosuars Movies</li>
<li>Add new Dinosuars species</li>
<li>Had Site</li>
</ul>
</li>
</ul>
</nav></asp:TableCell>
<asp:TableCell>
<nav class="menu" >
<ul>
<li><span class="mainmenu">Home page</span></li>
</ul>
</nav>
</asp:TableCell>
<asp:TableCell>
<nav class="menu" >
<ul>
<li><span class="mainmenu">Carnivores</span></li>
</ul>
</nav>
</asp:TableCell>
<asp:TableCell>
<nav class="menu" >
<ul>
<li><span class="mainmenu">Vegetarian</span></li>
</ul>
</nav>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
this is the c# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class DinoMenu : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Check_Click(object sender, EventArgs e)
{
SqlConnection c;
string str = "Data Source =(LocalDB)\\MSSQLLocalDB;";
str += "AttachDbFilename=|DataDirectory|\\DinoData.mdf;";
str += "Integrated Security= True";
c = new SqlConnection(str);
SqlCommand Cmd = new SqlCommand("SELECT COUNT(*) FROM [User] WHERE Pasword LIKE #Pasword AND Username LIKE #username;", c);
Cmd.Parameters.AddWithValue("#Pasword", pswrd.Text);
Cmd.Parameters.AddWithValue("#username", userid.Text);
c.Open();
int Userexist = (int)Cmd.ExecuteScalar();
c.Close();
if (Userexist > 0)
{
Response.Redirect("HaD.aspx", true);
}
else
{
ans.InnerText = "Eror Username and Password";
}
}
}
The problem you are having is because of this line:
<form name="login">
Delete this line and you will be ok.
If you have to gather the elements of your login form together, change it to a div instead
Explanation
HTML files cannot contain nested forms.
Every .aspx/.master file is already a form (That's the origin for the name Web Forms).
When you put a <form> inside one of these files you actually put your new form inside .NET's auto-created form

I need a textbox value in form 1 to be inserted into form2 as a string in the html level in asp

I would like to 'transfer' a textbox value into a html code. E.g the textbox value is a website like http://www.google.com in form1.aspx. and i would like the value to be put into the html code in form2.aspx . Might i ask how i should be going around on this?
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
Like i would like the textbox1 value to replace the current www.google.com
Here's how you can do this:
Webform1 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SOTest1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Your text: ">
</asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Webform1 Code:
using System;
namespace SOTest1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string myString = Server.HtmlEncode(TextBox1.Text);
Response.Redirect("Webform2.aspx?mystring=" + myString);
}
}
}
Webform2 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="SOTest1.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
</div>
</form>
</body>
</html>
Webform2 Code:
using System;
namespace SOTest1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mystring = Request.QueryString["mystring"];
myAnchor.HRef = mystring;
myAnchor.InnerText = mystring;
}
}
}

Only Content Controls are Allowed (C# webcontrols)

So, I'm not sure what's going on. My boss wasn't pleased with me using MVC and Razor, so I'm being forced to code with this nasty webcontrol/codebehind style. =/
The error is:
Only Content controls are allowed directly in a content page that contains Content controls.
Here is the masterpage:
<%# Master Language="C#"%>
<!DOCTYPE html>
<html>
<head>
<title>Application Form</title>
</head>
<body>
<div id="container">
<asp:contentplaceholder id="contentPlaceHolder" runat="server" />
</div>
</body>
</html>
And here is the Default.aspx page that's throwing the error.
<%# Page Language="C#" Inherits="dumb.Default" MasterPageFile="MasterPage.master" %>
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<p>Please complete this application.</p>
<form action="Default.aspx" method="post" runat="server">
<div>
<span class="spaced">Name:</span><asp:TextBox id="name" runat="server" />
</div>
</form>
<p>
<asp:Label id="finalmessage" runat="server" />
</p>
</asp:Content>
And the silly Default.aspx.cs codebehind...
using System;
using System.Web;
using System.Web.UI;
namespace dumb
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack) {
finalmessage.Text = "Submission Processed Successfully!";
}
}
}
}
All of your content, including static HTML must be inside Content tags in the content page. You have a <h2> outside:
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
Should be:
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<h2>Application Form</h2>

how to invoke class method asp.net C#

this code is a excerpt from the book asp.net all in one reference for dummies on pages 18 and 19. The problem with this is that it doesn't compile. The compiler says txtFirst, txtSecond and lblAnswer are not found in the current context. I am new in this field, could someone please help me out ?
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Simple Calculator</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<h1>The Simple Calculator</h1>
First number:
<asp:TextBox ID="txtFirst" runat="server" />
<br />
<br />
Second number:
<asp:TextBox ID="txtSecond" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" runat="server"
OnClick="btnAdd_Click" Text="Add" />
<br />
<br />
The answer is:
<asp:Label ID="lblAnswer" runat="server" />
</div>
</form>
</body>
</html>
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;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void btnAdd_Click(object sender,EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
}
}
Try this version instead - I've tested it and it works:
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Test.Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>The Simple Calculator</h1>
First number:
<asp:TextBox ID="txtFirst" runat="server" />
<br />
<br />
Second number:
<asp:TextBox ID="txtSecond" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" runat="server"
OnClick="btnAdd_Click" Text="Add" />
<br />
<br />
The answer is:
<asp:Label ID="lblAnswer" runat="server" />
</div>
</form>
</body>
</html>
CODE-BEHIND:
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;
namespace Test
{
public partial class Default : System.Web.UI.Page
{
protected void btnAdd_Click(object sender, EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
}
}
you must put your c# code in code behind page in solution explorer of Visual studio find Default.aspx.cs and copy
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;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void btnAdd_Click(object sender,EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
}
}
to the file I hope this help you
if you don't want to use separate .cs file then you have to use script tag in your aspx page.
<%# Page Language="C#">
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0
Transitional//EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<script runat="server">
protected void btnAdd_Click(object sender,EventArgs e)
{
decimal a = decimal.Parse(txtFirst.Text);
decimal b = decimal.Parse(txtSecond.Text);
decimal c = a + b;
lblAnswer.Text = c.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Simple Calculator</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<h1>The Simple Calculator</h1>
First number:
<asp:TextBox ID="txtFirst" runat="server" />
<br />
<br />
Second number:
<asp:TextBox ID="txtSecond" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" runat="server"
OnClick="btnAdd_Click" Text="Add" />
<br />
<br />
The answer is:
<asp:Label ID="lblAnswer" runat="server" />
</div>
</form>
</body>
</html>
Make two changes
In your aspx page, change to Inherits="WebApplication2.Default"
In your code behind change the class name to Default instead of _Default.
Then rebuild the solution.
Another reason could be copy-paste problem which sometimes messes up the designer file. Try deleting the controls and re-adding them manually.
Are you making a web site or a web application? If the latter, then try deleting (or removing/renaming, if you prefer to back it up first) the Default.aspx.cs file. Then right-click the Default.aspx file in Solution Explorer and select "Convert to Web Application".

Categories

Resources