Kamal Singh Kamal Singh - 1 year ago 92
C# Question

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">
<form id="form1" runat="server">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />


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;


pix pix
Answer Source

Did you try this from your C# code?

if (query.Any())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download