从数据库使用asp.net将值提取到另一个页面 [英] From the database fetch the value to another page using asp.net
问题描述
首先我创建数据库字段如下登录ID,Firstnmae,Lastnmae和密码
值如下1,ram,sam,123等表名输入。< br $>
First Design页面如下;
用户名txt_username(文本框)
密码txt_password(文本框)
插入(按钮)CLear(按钮)
当我输入用户名和输入表中的密码值,如登录ID 1和输入表中的密码,单击插入按钮,显示消息有效登录名和密码。当我输入错误的用户名和密码时,显示消息无效登录名和密码。
第二设计页面如下
名字txt_firstname(文本框)
姓氏txt_lastname(文本框)
当我从第一个设计页面点击插入按钮时,它转到第二个设计页面并输出为
在该页面中我希望输出为
名字ram
姓氏sam
来自条目表(数据库)。 />
oupput如何编写代码以及必须编写哪个页面代码请提及。对我来说非常有用。
第一个设计页面代码如下;
first i create database fields as follows Login Id,Firstnmae,Lastnmae and password
values as follows 1,ram,sam,123 etc table name entry.
First Design page as follows;
Username txt_username(textbox)
Password txt_password (textbox)
Insert (button) CLear(button)
when i type username and password values from the entry table such as login id 1 and password from the entry table and click the insert button it shows the message valid loginid and password.when i type wrong username and password it shows the message invalid loginid and password.
Second Design page as follows
Firstname txt_firstname(textbox)
Lastname txt_lastname(textbox)
when i click the insert buton from the first desing page it goes to second design page and output as
in that page i want the output as
Firstname ram
Lastname sam
From the entry table(database).
for the oupput how to write the code and in which page code has to be write please mention.it will be great useful for me.
First design page code as follows;
protected void Button1_Click1(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select LoginID,Password from entry where LoginID = '" + txt_username.Text + "' and Password = '" + txt_password.Text + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
Session["LoginID"] = txt_username.Text;
dr.Close();
con.Close();
Label3.Text = "Valid loginid and password";
Label3.ForeColor = System.Drawing.Color.DarkRed;
Server.Transfer("default2.aspx");
}
else
{
Label3.Text = "Invalid loginid and password";
Label3.ForeColor = System.Drawing.Color.DarkGreen;
}
}
请帮助我。
Please help me.
推荐答案
BUTTON1点击:
BUTTON1 CLICK:
protected void Button1_Click1(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from entry where LoginID = ''" + txt_username.Text + "'' and Password = ''" + txt_password.Text + "''", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
Session["LoginID"] = txt_username.Text;
string fname=dr.GetValue(1).ToString();//get the first name...
string lname=dr.GetValue(2).ToString();//get the last name...
Session["firstname"]=fname;//use session variable to send the first name
Session["lastname"]=lname;//use session variable to send the last name
dr.Close();
con.Close();
Label3.Text = "Valid loginid and password";
Label3.ForeColor = System.Drawing.Color.DarkRed;
Server.Transfer("default2.aspx");
}
else
{
Label3.Text = "Invalid loginid and password";
Label3.ForeColor = System.Drawing.Color.DarkGreen;
}
}
at Second Page_Load() design 2Textbox
string a=Session["firstname"].ToString();//get the session variable & assign to a
string b=Session["lastname"].ToString();//get the session variable & assign to b
textbox1.text=a;
textbox2.text=b;
使用会话,如
use session like
protected void LoginButton_Click(object sender, EventArgs e)
{
ValidateUser(txtUserName.Text.Trim(), txtPassword.Text.Trim());
Session["fname"]=txtUserName;//use session variable to send the first name
Session["lastname"]=txtlastName;//use session variable to send the last name
}
private void ValidateUser(string user, string pwd)
{ SqlConnection conn =new SqlConnection();
conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ROKER_S (Dgvbv)\OnlineTestWEB\OnlineTestWEB\App_Data\TestEXAM.mdf;Integrated Security=True;User Instance=True");
string sqlcmd="select * from LoginTable where UserName=@UserName and Password=@Password";
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
cmd.Parameters.AddWithValue("@UserName", user);
cmd.Parameters.AddWithValue("@Password", pwd);
conn.Open();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DataSet DS = new DataSet();
cmd.ExecuteScalar();
DA.Fill(DS);
if (DS.Tables[0].Rows.Count > 0)
{
Response.Redirect("Default.aspx");
}
else
{
Response.Write("Invalid UserID Or Password");
}
conn.Close();
}
}
现在第2页使用像......
now at 2nd page use like...
at Second Page_Load() design 2Textbox
string a=Session["firstname"].ToString();//get the session variable & assign to a
string b=Session["lastname"].ToString();//get the session variable & assign to b
label1.text=a;
label2.text=b;
这篇关于从数据库使用asp.net将值提取到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!