CS0103: The name does not exist in the current context - c#

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...

Related

CS0103 the name 'txtname' does not exist in the current context

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"

i am trying to run a asp.net method with an html submit button but method would not run?

I want the method submit_click to be activated when the submit button on the HTML part is pressed
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" method="post" onsubmit="Submit_Click">
mail<br />
<asp:TextBox ID="mail" runat="server" Style="margin-left: 0px">mail</asp:TextBox>
<br />
name<br />
<asp:TextBox ID="name" runat="server" Width="117px">name</asp:TextBox>
<br />
last
<br />
<asp:TextBox ID="last" runat="server">name</asp:TextBox>
<p>
pass
</p>
<p>
<asp:TextBox ID="password" runat="server">password</asp:TextBox>
</p>
id <p>
<asp:TextBox ID="id1" runat="server">id</asp:TextBox>
</p>
<input id="Submit" type="submit" value="submit" onserverclick="Submit_Click()" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0; Data source = " + Server.MapPath("") + "\\Database.accdb");
conn.Open();
string com = "INSERT into myusers (myid,myname,mymail,mypass,mylast) VALUES ('" + id1.Text + "," +name.Text + "," + mail.Text + "," +password.Text + "," + last.Text + "')";
OleDbCommand comm = new OleDbCommand(com, conn);
comm.ExecuteNonQuery();
conn.Close();
}
}
Actually why you're doing this , that i don't know because Asp Button will also converted like below on browser side, Still if you want to use it put runat="server", try below code.
<input id="Submit" runat="server" type="submit" value="submit" onserverclick="Submit_ServerClick" />
You are missing 'runat="server"'. Include it in the submit control.

I need to check whether the data entered in form exists in database or not and if it exists redirect it to another page

My short query is that i need to check the login form data that the value entered in name by user to be checked against all the values under column name in the database using LINQ query expression only and if it exists i want to select that particular row to which the name field matches with and redirect the user to another Dashboard page.
Below is the code which stores and selects the row if the record is found in the database into a variable query.Now it's not able to check whether the variable in query points to any row if selected or not of the database table.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="SessionHandling.aspx.cs" Inherits="WebApplication5.SessionHandling" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
THIS IS LOGIN FORM
</h1>
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<br />
E-mail:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication5
{
public partial class SessionHandling : System.Web.UI.Page
{
DataClasses1DataContext db = new DataClasses1DataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
tblContact tb = new tblContact();
var query = from x in db.tblContacts where x.name == txtName.Text select x;
}
}
}
Did you try this from your C# code?
if (query.Any())
{
Response.Redirect("http://www.microsoft.com")
}
else
{
Response.Redirect("http://www.google.com")
}

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".

passing value from ASP.NET to Java Scripts to HTML

I need value to be passing from ASP.NET page to JavaScript and then to HTML textfield.
My following code is able to read values from ASP.NET to JavaScript but unable to pass value form JavaScript to HTML TextField. Can anybody please correct my code.
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function can() {
var myVariable = '<%=ServerSideVariable %>';
document.write(myVariable);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" value='can()'/>
</div>
</form>
</body>
</html>
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 _Default : System.Web.UI.Page
{
public string ServerSideVariable { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
string pval = "Passed value;
ServerSideVariable = pval;
}
}
document.getElementById('Text1').value = myVariable;
But you need to move the assignment script block bellow the declaration of element Text1, or do it on document.ready
Anyway, you also need to error check.
It's best to use JQuery anyway, and get familiar with it.
As others have said with javascript OR like this:
(since there is nothing in your example that requires javascript -- KISS-- don't use javascript if you don't have to do so.)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" ><%=ServerSideVariable %></input>
</div>
</form>
</body>
</html>
Do this instead in a document.load.
document.getElementByID('Text1').value = myVariable;

Categories

Resources