Click to See Complete Forum and Search --> : taking data from database


Lord
03-26-2009, 01:16 PM
Hi friends, I am newbeginner about database on ASP.NET, and trying to take datas from my database named SecData, and printing a data to a Label. I've worked on it about a week but I COULDNT DO IT!! Please help me friends

My Problem is:
1) I got a database named as "SecData",
2) and SecData has only one table named "UserData" that has two column named "Username" and "Password"
3) And finally, there is a user that has "Sean" as a Username, and "12345" as a Password.

My question is:
I need to select a user named Sean, and take his password as a string, and print into a label. I have to do it, please help friends please

My Code is:
Try.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Try.aspx.cs" Inherits="Try" %>
<!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>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Read" />
</div>
<asp:Label ID="Label1" runat="server" Font-Names="Verdana" Text="Password"></asp:Label>
</form>
</body>
</html>

Try.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 Try : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void Button1_Click(object sender, EventArgs e)
{
SecDataDataContext sec = new SecDataDataContext();

var user = from chosen in sec.UserDatas
where chosen.Username=="Sean"
select chosen.Password;

Label1.Text = what should I write here?? }
}

eclipsed4utoo
03-27-2009, 08:56 AM
this should work.


Label1.Text = user.ToString();


the ".ToString()" may not be needed since the "var" datatype will dynamically set it's datatype. So it may already be a string.

Lord
03-27-2009, 09:50 AM
Unfortunately it did NOT worked :(

http://img216.imageshack.us/img216/4461/erroru.jpg

this is 8th day that I couldnt solve this problem, please help me friends :(

Lord
03-27-2009, 10:55 AM
hey I solved the problem at last!!!!!!!!!!!!!!!!!!!!!!!!!!

I tried all the combinations with "users" and this code worked!!

Label1.Text = users.First();
but what is the ".First()" doing? What's the meaning of it?

Hack
03-27-2009, 12:53 PM
I couldn't even find Users.First through Google so I'm not sure what is going on here.