登录页面中可见第二种形式后的隐形第一种形式 [英] Invisible first form after visible second form in login page

查看:70
本文介绍了登录页面中可见第二种形式后的隐形第一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种窗口形式。首先是登录表单,第二个是.net中的主要表单。

一旦我输入用户名和密码,所以第二个表单打开但第一个表单(登录表单不隐藏。它仍然可见)

i多次应用了方法和属性,但它在我的代码中不起作用。请给我一个正确的解决方案



我尝试过:



  private   void  button1_Click(对象发​​件人,EventArgs e)
{
尝试
{
con = new SqlConnection( 数据源= .;初始目录= DCID;集成安全性=真正的);
string query = select * from login其中username =' + textBox1.Text + '和password =' + textBox2.Text + ';
cmd = new SqlCommand(query,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);


if (dt.Rows [ 0 ] [< span class =code-string>
username]。ToString()== textBox1.Text&& dt.Rows [ 0 ] [ password]。ToString( )== textBox2.Text)
{


Form6 obj = new Form6();
Form1 objform1 = new Form1();



obj.Show();
objform1.Visible = false ;
objform1.Hide();
visiblemethod();

textBox2.Text = ;
textBox1.Text = ;



}
else if (dt.Rows [ 1 ] [ username ]。ToString()== textBox1.Text&& dt.Rows [ 1 ] [ password]。ToString()== textBox2.Text)
{
// Form4 obj4 = new Form4();
// obj4.Show();
clearall();

}
else
{
MessageBox.Show( 匹配不一样);
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}


}

解决方案

对于初学者,不要这样做就像那样!

这里有很多错误,很难知道从哪里开始!

让我们选择真正重要的:SQL注入。

不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。请参阅此处: xkcd:对妈妈的漏洞利用 [ ^ ]



第二个几乎一样大:从不以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ]。

见这里: http://www.commitstrip.com/wp-content/uploads/2013/09/Strips-Erreur-au-pilori-001-650-finalenglish.jpg [ ^ ]



接下来,不要对连接字符串进行硬编码。在将代码发布到生产环境之前,它们很难对其进行测试!请改用设置或配置文件。



现在我们遇到你注意到的问题。

 Form6 obj =  new  Form6(); 
Form1 objform1 = new Form1();
obj.Show();
objform1.Visible = false ;
objform1.Hide();
visiblemethod();

为什么要创建表单的新实例以便在它永远不显示时隐藏它?

请尝试这样做:

 Form6 obj =  new  Form6(); 
obj.Show();
隐藏();
visiblemethod();

这将隐藏表单的当前实例 - 即用户输入的实例 - 而不是全新的...



BTW:帮自己一个忙,并停止使用Visual Studio默认名称 - 你可能还记得今天的TextBox8是手机号码,但是当你必须在三周内修改它时,你呢?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......


我认为你需要在 button1_Click()方法之外定义你的Forms变量。 />
最好在Class定义之后

 Public Form6 obj =  new  Form6(); 


你在第二行做错了。无需创建form1的对象。

 Form6 obj =  new  Form6(); 
Form1 objform1 = new Form1();

obj.Show();
objform1.Visible = false ;
objform1.Hide();
visiblemethod();

textBox2.Text = ;
textBox1.Text = ;



以下是建议代码

 Form6 obj =  new  Form6(); 
obj.Show();
this .Hide();
visiblemethod();


i have two window forms. first is login form and second one is main form in .net.
once i enter userid and password so second form opens but first one (login form does not hide. it remains visible )
i have applied both method and property many times but it does not work in my codes. please give me a right solution

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection("Data Source=.;Initial Catalog=DCID;Integrated Security=True");
                string query = "select * from login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'";
                cmd = new SqlCommand(query, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
               

                if (dt.Rows[0]["username"].ToString() == textBox1.Text && dt.Rows[0]["password"].ToString() == textBox2.Text)
                {

                   
                    Form6 obj = new Form6();
                    Form1 objform1 = new Form1();

                 
                  
                    obj.Show();
                    objform1.Visible = false;
                    objform1.Hide();
                    visiblemethod();
                   
                    textBox2.Text = "";
                    textBox1.Text = "";
                    


                }
                else if (dt.Rows[1]["username"].ToString() == textBox1.Text && dt.Rows[1]["password"].ToString() == textBox2.Text)
                {
                    //Form4 obj4 = new Form4();
                    //obj4.Show();
                    clearall();
                  
                }
                else
                {
                    MessageBox.Show("Match does not same");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

           
        }

解决方案

For starters, don't do it like that!
There are so many things wrong here it's difficult to know where to start!
Let's go with the really big one: SQL Injection.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. See here: xkcd: Exploits of a Mom[^]

The second one is nearly as big: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^].
See here: http://www.commitstrip.com/wp-content/uploads/2013/09/Strips-Erreur-au-pilori-001-650-finalenglish.jpg[^]

Next, don't hardcode connection strings. They make it very difficult to test your code before you release it to production! Use settings or configuration files instead.

Now we get to the problem you have noticed.

Form6 obj = new Form6();
Form1 objform1 = new Form1();
obj.Show();
objform1.Visible = false;
objform1.Hide();
visiblemethod();

Why are you creating a new instance of a form in order to hide it, when it is never displayed?
Try this instead:

Form6 obj = new Form6();
obj.Show();
Hide();
visiblemethod();

That will hide the current instance of the form - i.e. the one the user typed in - rather than a brand new one...

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...


I think you need to define your Forms variables outside of the button1_Click() method.
Preferably right after the Class definition as

Public Form6 obj = new Form6();


You are doing wrong in second line. No need to create object of form1.

Form6 obj = new Form6();
Form1 objform1 = new Form1();

obj.Show();
objform1.Visible = false;
objform1.Hide();
visiblemethod();

textBox2.Text = "";
textBox1.Text = "";


Here is suggested code

Form6 obj = new Form6();
obj.Show();
this.Hide();
visiblemethod();


这篇关于登录页面中可见第二种形式后的隐形第一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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