简体   繁体   English

如何在欢迎页面上显示用户名

[英]How to show username on the welcome page

I have two forms Form1 and Form2.我有两个表格 Form1 和 Form2。 Form1 contains two textBoxs for login details and Form2 contain a label just to show the username. Form1 包含两个用于登录详细信息的文本框,Form2 包含一个标签仅用于显示用户名。 I have this code but it doesn't work.我有这个代码,但它不起作用。 Thanks in advance.提前致谢。

Form1表格1

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string constring = @"Data Source=.;Initial Catalog=POS;Integrated Security=True";
        SqlConnection con = new SqlConnection(constring);
        con.Open();
        SqlCommand sqlcmd = new SqlCommand("Select firstname, password from credentials", con);
        SqlDataReader dr = sqlcmd.ExecuteReader();

        while (dr.Read())
        {
            string username = usernametxt.Text;
            string password = passwordtxt.Text;
            if (dr["username"].ToString() == username && dr["password"].ToString() == password)
            {
                Form2 hmpage = new Form2(username);

                hmpage.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("The username or password you entered is incorrect!", "Logon Message!");
            }
        }
        con.Close();
        dr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Form2表格2

public Form2(string strTextBox)
{
    InitializeComponent();
    label1.Text = "Welcome to:(" + strTextBox + ")";
}

It would be better if you add breakpoints to some statements of your code for you to trace why it doesn't work.如果您在代码的某些语句中添加断点,以便跟踪为什么它不起作用,那就更好了。 (press F5.. then F11 after adding breakpoints). (添加断点后按 F5.. 然后按 F11)。

By then, you can add global variable, which can reside at Program.cs and assign the retrieved username.届时,您可以添加全局变量,该变量可以驻留在 Program.cs 并分配检索到的用户名。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查角色,用户名和密码,并在欢迎页面mvc5上重定向 - check role,username and password and redirect on welcome page mvc5 登录后如何在下一个表格的标签上显示欢迎用户名? - How to display welcome username in a label on next form after login? 我在 c# 上创建了一个登录页面,当我输入用户名和密码时,我需要打印欢迎来到我的页面。 这怎么可能? - i have created a login page on c# and i need to print welcome to my page when i enter username and password. How is it possible? 如何在 WebAPI 中使用 Swagger 作为 IAppBuilder 的欢迎页面 - How to use Swagger as Welcome Page of IAppBuilder in WebAPI 如何在显示 winforms 应用程序的主屏幕之前显示欢迎屏幕? - How to show a welcome screen before the main screen of a winforms application is shown? 如何获取SharePoint 2010网站的默认/欢迎页面? - How Do I Get The Default/Welcome Page Of A SharePoint 2010 Site? 如何访问受用户名密码保护的网页并将其显示在webview中 - How can I access a web page protected with username password and show it in webview 如何在母版页上创建用户名 - How to create username on master page 在MVC环境中,如何在我的母版页顶部显示“欢迎使用“ CurrentUserName””? - How do I show Welcome “CurrentUserName” at the top of my MasterPage in MVC environment? 如何在 Unity3D 上为 android 显示 Google Play 游戏“欢迎回来”祝酒词? - How to show a Google Play Games “Welcome Back” toast for android on Unity3D?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM