在Page_load上将数据库值分配给文本框 [英] Assigning database values to textbox on Page_load

查看:85
本文介绍了在Page_load上将数据库值分配给文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我有登录页面,在成功登录时将用户重定向到另一个页面。我想设置这样一个功能,当用户看到重定向的页面时,应该显示员工姓名和员工代码(页面是正在重定向登录)。所以我使用查询字符串将登录页面的用户名字段传递给重定向页面,并基于获取数据库值并将它们分配给文本框。 但它没有按预期工作,它没有在页面加载时显示名称和代码......我的代码在下面...... 任何帮助......都会非常有用......



Hello everyone.I have login page,which redirects user to another page on successfull login.I want to set such a functionality that,EMPLOYEE NAME AND EMPLOYEE CODE should be displayed as and when the user sees the redirected page(page that is being redirected on login).so i am passing the username field of the login page to redirected page using query string and based on that fetching database values and assigning them to textboxes. But it is not working as expected,it doesnt displays name and code on page load...my code is below...any help...would be greatly appriciated...

protected void Page_Load(object sender, EventArgs e)
        {
            
            string str = Request.QueryString["uname"];
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select name,code from emp_details where uname='" + str + "'", conn);
          
            SqlDataReader dr = cmd.ExecuteReader();
            
                name.Text = dr["name"].ToString();
                code.Text = dr["code"].ToString();
           

        }

推荐答案

使用此

use this
protected void Page_Load(object sender, EventArgs e)
{

string str = Request.QueryString["uname"];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select name,code from emp_details where uname='" + str + "'", conn);
DataSet ds=new DataSet();
da.fill(ds);
name.Text = ds.Tables[0].Rows[0]["name"].ToString();
code.Text = ds.Tables[0].Rows[0]["code"].ToString();

 
}


你好



你的代码似乎没错。但是你需要一些过滤器

首先检查你的查询字符串是否为空或空或其他格式



之后通过它作为Sql参数使用Parameter.AddwithValue





和上次检查你的读者HasValue与否





之后你得到你的代码有什么问题?

然后将它的值分配到你的文本框中..





我希望你的问题能解决,如果不是请你给你的评论,
Hello

Your code not seem wrong But you take some filter
like first check your query string is not null or empty or other format

after that pass it as Sql Parameter using Parameter.AddwithValue


And Last Check your Reader HasValue or not


After That you get What Problem Your Code Have?
Then Assign Its Value into Your TextBox..


I Hope Your Problem Will Solve IF Not Please give your Comment,,


我认为这是ASP.NET虽然你没有比如说 ?你的意思是没有按预期工作?这段代码很可怕,我可以使用这个页面来删除因SQL注入而导致的数据库。但是,此代码将继续逐行读取并反复设置文本。如果您的最后两个为空,则值将为空。你了解这段代码吗?
I assume this is ASP.NET although you did not say that ? What do you mean ''not working as expected'' ? This code is horrendous, I can use this page to erase your DB due to SQL injection. However, this code will keep reading row after row and setting the text over and over. If your last two is blank, then the values will be blank. Do you understand this code ?


这篇关于在Page_load上将数据库值分配给文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆