如何防止当前用户从数据库中删除自己? [英] How to prevent currently loged in user from deleting himself from database?
本文介绍了如何防止当前用户从数据库中删除自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含用户名和密码和类型的数据表,以及一个用于编辑用户的gridview。现在,当我单击删除用户按钮时,我希望它检查当前用户名和所选行用户名是否相同。我有一个名为kullaniciadi的字符串,我在textbox1.text的用户名中保留最后一个字符串:
kullaniciadi = UserNametextBox1.Text;
我试过这个,但是它不能用于非常显而易见的原因我在网上找不到任何关于这个的东西:
if (log_InDataGridView.CurrentCell.Value.ToString == kullaniciadi)
MessageBox.Show( 无法删除当前用户;
我该怎么做?
Thanx。
解决方案
CurrentCell与使用SelectedRows属性的所选单元格不同,并从那里访问用户名列。
这有效:
private 无效对接on5_Click( object sender,EventArgs e)
{
string tempusername;
if (log_InDataGridView.Rows.Count > 1 )
{
if (log_InDataGridView.Rows [log_InDataGridView.CurrentRow.Index] .IsNewRow!= true )
{
tempusername = log_InDataGridView.CurrentRow.Cells [ 0 ] .Value.ToString( ).Trim()ToLower将();
username = username.Trim()。ToLower();
if (tempusername.Equals(username)== true )
{
MessageBox.Show( 无法删除当前用户);
}
else
this .log_InBindingSource.RemoveCurrent();
}
}
else
MessageBox.Show( 请先选择要删除的用户);
}
I have a data table contains username and password and type, and a gridview to edit users. Now when I click the delete user button I want it to check if the current user name and selected rows user name is same or not. I have a string called kullaniciadi where I keep last loged in username from textbox1.text like this:
kullaniciadi = UserNametextBox1.Text;
And I tryed this but well it doesn't work for apearently obvious reasons couse I couldn't find anything on the internet about this:
if ( log_InDataGridView.CurrentCell.Value.ToString == kullaniciadi)
MessageBox.Show ( "Cannot delete currently loged in user" );
How can I do this?
Thanx.
解决方案
The CurrentCell is not the same as the selected cell- try using the SelectedRows property instead and access the username column from there.
This worked:
private void button5_Click ( object sender , EventArgs e ) { string tempusername; if ( log_InDataGridView.Rows.Count > 1 ) { if ( log_InDataGridView.Rows [ log_InDataGridView.CurrentRow.Index ].IsNewRow != true ) { tempusername= log_InDataGridView.CurrentRow.Cells [ 0 ].Value.ToString ().Trim().ToLower(); username= username.Trim ().ToLower (); if( tempusername.Equals(username)==true) { MessageBox.Show ( "Cannot delete currently loged in user" ); } else this.log_InBindingSource.RemoveCurrent (); } } else MessageBox.Show ( "Please select a user to delete first" ); }
这篇关于如何防止当前用户从数据库中删除自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文