html master page with data base sql server - c#

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

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"

WebForms keeps showing Event validation error on post

When I try to submit information from client to server, I receive Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/>. I've tried most of the suggestions in the answers and they don't seem to be helping.
At the top of the .aspx page in question, adding EnableEventValidation="false" does solve the issue. However, I would like to this to remain as set to true.
I've also added (!Page.IsPostBack) in the code behind and this is still causing issues.
Could it be the jQuery timpicker that's causing the issue by inputting values based on what the user selects?
Code behind .cs
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var DbHelper = new DbHelper();
listOfUsers.DataSource = DbHelper.UserList();
listOfUsers.DataBind();
}
else
{
string test = listOfUsers.SelectedValue;
string time = timePicker.Text;
string reason = ReasonForRemoval.Text;
}
}
}
.aspx file
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="xxxMyProjectNamexxx" EnableEventValidation="true"%>
<%--<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>--%>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js' type='text/javascript'></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="/Scripts/jquery-ui-timepicker-addon.js"></script>
<link rel="stylesheet" href="/Styles/jquery-ui-timepicker-addon.css">
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('[id*=timePicker]').timepicker({
timeFormat: "hh:mm:ss"
});
});
</script>
<head>
<h2>
My App
</h2>
<p>
Enter relevant info
</p>
</head>
<body>
<form method="post">
<div>
<label for="UserName">User Name:</label>
<asp:DropDownList ID="listOfUsers" runat="server"></asp:DropDownList>
</div>
<br/><br/>
<table>
<tr>
<th>
<div>
<label for="Time">Time:</label>
<asp:TextBox ID="timePicker" runat="server"></asp:TextBox>
</div>
</th>
</tr>
</table>
<br/><br/>
<div>
<label for="Reason">Reaso:</label>
<br/>
<asp:TextBox ID="ReasonForRemoval" runat="server" TextMode="MultiLine" Rows="5" Width="400px" style="resize:none"></asp:TextBox>
</div>
<br/><br/>
<div>
<label> </label>
<input type="submit" value="Submit" class="submit" />
</div>
</form>
</body>
</asp:Content>
DbHelper.cs used to generate list
public class DbHelper
{
private EntityConnection entCon;
public DbHelper()
{
entCon = new EntityConnection(ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString);
}
public List<string> UserList()
{
List<string> userList = new List<string>();
using(SqlConnection conn = (SqlConnection)entCon.StoreConnection)
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [MyDatabase].[dbo].[users]";
using (SqlDataReader objReader = cmd.ExecuteReader())
{
if (objReader.HasRows)
{
while (objReader.Read())
{
userList.Add(Convert.ToString(objReader[0]));
}
}
}
}
return userList;
}
}
}

Can';t work using html textbox to login

Because I am using html buttons and textbox to login, I must do the code behind in the javascript in the source code in order to do the code behind. Whether I login using the correct username and password which is Admin and 123 and click the login button, or I type in nothing and click the login button, it always redirects me to ResultDetails.aspx. That means login fail. Login will pass if it redirect me to Search.aspx. What's Wrong? Even if I change .Value to .Text, it is still the same effect
MY SOURCE CODE
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!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>
<style type="text/css">
</style>
<link rel="stylesheet" type="text/css" href="stylesheets/loginstyle.css" />
<script language="javascript" type="text/javascript">
// <![CDATA[
function Button1_onclick() {
if (txtUserName.Value == "Admin" && txtPassword.Value == "123") {
//Login as Hardcoded User
//Do your stuff
window.location.assign("Search.aspx")
}
else {
window.location.assign("ResultDetails.aspx")
}
}
// ]]>
</script>
</head>
<body>
<div id="wrapper">
<form name="login-form" class="login-form" action="" method="post">
<div class="header">
<h1>Login Form</h1>
<span>Fill out the form below to login to my super awesome imaginary control panel.</span>
</div>
<div class="content">
<input name="username" type="text" class="input username" placeholder="Username" runat="server" id="txtUserName" />
<div class="user-icon"></div>
<input name="password" type="password" class="input password" placeholder="Password" runat="server" id="txtPassword" />
<div class="pass-icon"></div>
</div>
<div class="footer">
<input type="button" name="submit" value="Login" class="button" runat="server" id="Button1" önserverclick="Button1_Click" onclick="return Button1_onclick()" />
</div>
</form>
</div>
<div class="gradient"></div>
</body>
</html>
MY CODE BEHIND CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
You are not using asp.net server controls to access values like
txtUserName.Value
Rather, you are using Javascript on HTML Controls. So, your syntax should be like:
if (document.getElementById("txtUserName").value == "Admin" && document.getElementById("txtPassword").value == "123")
You need to access the fields properly
e.g.
if (document.getElementById('txtUserName').value == "Admin" && document.getElementById('txtPassword').value == "123")
If you are using Jquery lib in your code
You can also do
if($.("#txtUserName").val()=="Admin" && $.("#txtPasword").val()=="123")
You can try below code which fetches the value from text boxes using the id in JavaScript.
function Button1_onclick() {
if (document.getElementById('txtUserName').value == "Admin" && document.getElementById('txtPassword').value == "123") {
//Login as Hardcoded User
//Do your stuff
window.location.assign("Search.aspx")
}
else {
window.location.assign("ResultDetails.aspx")
}
}
if (document.getElementById("txtUserName").value == "Admin"
&& document.getElementById("txtPassword").value == "123")

javascript pageLoad function not firing in asp.net Site.Master

I am trying to get this javascript function to fire every time there is a postback in my ASP.net project using c#. I read online that if you create a pageLoad() function in Javascript, it will fire every time on a postback. I can't get it to fire. Here is all the code in my Site.Master file.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Weights.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<script type="text/javascript" src="<%=ResolveUrl("~/Scripts/jquery-1.4.1.min.js")%>"></script>
<script type="text/javascript">
function pageLoad() {
if ($('#MainContent_ckbIncludeWeight').checked) {
$('#MainContent_txtPalletWeight').show();
console.log('it is checked!');
alert('it is checked!');
}
if (true) {
console.log('fire!');
alert('fire!');
}
}
</script>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ Log In ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
<script type="text/javascript">
function pageLoad() {
if ($('#MainContent_ckbIncludeWeight').checked) {
$('#MainContent_txtPalletWeight').show();
console.log('it is checked!');
alert('it is checked!');
}
if (true) {
console.log('fire!');
alert('fire!');
}
}
$(document).ready(function () {
$('#MainContent_ckbIncludeWeight').click(function () {
if (this.checked) {
$('#MainContent_txtPalletWeight').show();
$('#MainContent_txtPalletWeight').val('40');
$('#MainContent_txtPalletWeight').after("<span id='pound'>#</span>");
}
else {
$('#MainContent_txtPalletWeight').hide();
$('#MainContent_txtPalletWeight').val('');
$('#pound').remove();
}
});
});
</script>
</html>
I would appreciate any help.
Thanks
Mike
You're already using jQuery's $(document).ready, just add your code there as well. It will run every time page reloads:
$(document).ready(function () {
$('#MainContent_ckbIncludeWeight').click(function () {
if (this.checked) {
$('#MainContent_txtPalletWeight').show();
$('#MainContent_txtPalletWeight').val('40');
$('#MainContent_txtPalletWeight').after("<span id='pound'>#</span>");
}
else {
$('#MainContent_txtPalletWeight').hide();
$('#MainContent_txtPalletWeight').val('');
$('#pound').remove();
}
});
// Originally from function pageLoad()
if ($('#MainContent_ckbIncludeWeight').checked) {
$('#MainContent_txtPalletWeight').show();
console.log('it is checked!');
alert('it is checked!');
}
if (true) {
console.log('fire!');
alert('fire!');
}
});
You have to have ScriptManager on page in order for pageLoad() to work.
There is however no need for adding it, if you just want to run some script on every postback.
You can do something like putting this:
<asp:PlaceHolder ID="phPageLoad" runat="server">
<script type="text/javascript">
if ($('#MainContent_ckbIncludeWeight').checked) {
$('#MainContent_txtPalletWeight').show();
console.log('it is checked!');
alert('it is checked!');
}
if (true) {
console.log('fire!');
alert('fire!');
}
</script>
</asp:PlaceHolder>
into your master page and in codebehind of master page add this to Page_Load:
phPageLoad.Visible = Page.IsPostback;

Can't display results where the ending matches

I'm making a rhyme dictionary, and I have a database table with 3 coloumns, the user will search the database and the search keywords will search database where the ending of the word matches with the word in textbox.
When I enter some keyword into textbox
I get ERROR: Incorrect syntax near the keyword 'LIKE'.
This is how my database looks like
Here is how my aspx looks like:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Kafiye Dizini - Türkçe Kafiye Bulma Sözlüğü - Uyak Bulucu Sözlük - İstediğiniz harf ile biten kelimeleri bulan sözlük</title>
<meta name="description" content="İstediğiniz harfler ile biten kelimeleri bulmanızı sağlayan sözlük" />
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<link href="style.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="top">
<div class="email">İletişim: fahrettinveysel#gmail.com</div>
</div>
<div class="leftcontainer">
</div>
<div class="middlecontainer">
<div class="title">Kafiye Dizini</div>
<div class="subtitle">İstediğiniz harf veya hece ile biten kelimeleri bulmanızı sağlayan sözlük</div>
<div class="searchcontainer">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
<div class="resultboxcontainer">
<div id="resultbox1" runat="server"></div>
<div id="resultbox2" runat="server"></div>
<div id="resultbox3" runat="server"></div>
</div>
<div class="idefix"></div>
</div>
<div class="rightcontainer">
<div class="ornekarama">
<div class="ornekaramabaslik">Örnek Arama</div>
<input type="text" class="ornekaramatextbox" value="rop" disabled="disabled" />
<div class="ornekaramasonuclar">filantrop<br />gardırop<br />hipermetrop<br />mikrop<br />mizantrop</div>
</div>
</div>
</div>
</form>
</body>
</html>
and this is my aspx.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection cnn = new SqlConnection("Initial Catalog=kafiyedizini;Data Source=localhost;Integrated Security=SSPI;");
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT kelime1,kelime2,kelime3 FROM kelimeler LIKE #arama", cnn);
cmd.Parameters.AddWithValue("#arama", "%" + TextBox1.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
resultbox1.InnerHtml += dr.GetString(0);
resultbox2.InnerHtml += dr.GetString(1);
resultbox3.InnerHtml += dr.GetString(2);
}
}
cnn.Close();
}
else
{
resultbox1.InnerHtml += "please enter data";
}
}
}
The initial sql statement in your question should look like this
SELECT kelime1,kelime2,kelime3 FROM kelimeler where kelime1 LIKE #arama OR kelime2 LIKE #arama or kelime3 like #arama
You missed the where and the fields you want to use in your like statement.
to have each result in a separate 'box' you better investigate how a GridView works or a DataRepeater.
Closest in your initial code what could work, including support for handling null/emtpy values for one the fields returned, nicely filling the 3 resultboxes:
var f1 = dr.GetString(0);
var f2 = dr.GetString(1);
var f3 = dr.GetString(2);
if (!String.IsNullOrEmpty(f1))
resultbox1.InnerHtml += String.Format("<div>{0}</div>",f1);
if (!String.IsNullOrEmpty(f2))
resultbox2.InnerHtml += String.Format("<div>{0}</div>",f2);
if (!String.IsNullOrEmpty(f3))
resultbox1.InnerHtml += String.Format("<div>{0}</div>",f3);
You are missing WHERE part of the SQL query
Example:
SELECT * FROM test WHERE test.Id LIKE '%asd%'
I also think, dr.Read() executes PER ROW.
Hope this helps

Categories

Resources