如何使用csharp中的combobox检查记录已经存在 [英] how to check record already exists using combobox in csharp

查看:53
本文介绍了如何使用csharp中的combobox检查记录已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合框如下;





名称组合框

天组合框



从数据库中检索并显示在组合框中。

在日期栏中手动输入星期六的代码星期六。
在运行模式下


我选择名称并选择日期并单击保存按钮。记录保存在数据库中。它工作正常。



例如如下;



名字Suresh

Days Sunday



以上记录保存在数据库中。



然后我有一个搜索按钮,当我点击数据库中的搜索按钮时,数据记录显示在datagridview中,当我点击datagridview时,特定记录会显示在相应的组合框中。



假设当我选择suresh并选择星期日并单击保存按钮时,该时间验证,对于suresh星期日已经存在。



表示如何验证Combobox。使用csharp。



请帮帮我。





谢谢&Rgds ,

Narasiman P.

i have two combobox as follows;


Name combobox
Days combobox

in the name retrieved from the database and display into the combobox.
in the days column manually typed in the code sunday to saturday.

in the run mode i select the name and select the days and click the save button. the records are saved in the database. it is working fine.

for example as follows;

Name Suresh
Days Sunday

the above record is saved in the database.

then i have one search button,when i click the search button from the database records are displayed in the datagridview,when i click the datagridview, the particular record is displayed in the respective combobox.

suppose when i select the suresh and select the sunday and click save button, that time validate ,For suresh sunday is already exists.

for that how to validate the Combobox.using csharp.

please help me.


Thanks & Rgds,
Narasiman P.

推荐答案

尝试在保存按钮后面放置一个sql查询,例如
Try placing a sql query behind the Save button e.g.
sql = "select from table where name='" + Name.Text + "' and days = '" + Days.Text + "'"
etc...

然后如果是SqlDataReader.HasRows(或者你正在使用的任何东西) )返回true你知道组合已经存在。

显示错误信息但不执行保存



(NB我没有为了懒惰而在这里做了但是你应该使用正确的参数化查询而不是像我在这里那样插入文本框的内容。

then if the SqlDataReader.HasRows (or whatever you are using) returns true you know the combination already exists.
Display an error message and don''t perform the save

(NB I haven''t done it here for the sake of being lazy but you should really use proper parameterised queries rather than inserting the content of textboxes as I''ve done here)


按钮点击事件中


hi


您必须编写此代码....



hi
in button click event you have to write this code....

 protected void btnsave_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
 MySqlCommand msql = new MySqlCommand("select from tablename where name=''" +Name_combobox.Text + "'' and days = ''" + Days_combobox.Text + "'' ", con);
            MySqlDataReader mdr = msql.ExecuteReader();
            if (mdr.Read())
            {
                mdr.Close();
                msgshow.Visible = true;// this is a label control
                msgshow.Text = "Duplicate entry found";
            }
            else
            {
               Do here your Insert Code....
                   
            } 
        }
Catch(Exception ex)
{
msgshow.text=ex.message; 
}
}





谢谢乐于帮助...



Thanks Happy to help...


最好的想法是你必须为每个名字都有一个唯一的id(主键),以便你可以轻松区分。

在combobox的离开事件中,你可以使用SqlDataReader从数据库中读取数据并使用循环比较字符串...

简单示例:

the best idea is u must have an unique id (Primary key) for each name so that u can distinguish easily.
on the leave event of combobox, u can use SqlDataReader to read the data from database and compare the strings using a loop...
simple example:
int i = 0, index;
            string dbUserName;
            string dbPassword;
            string username = txtUserName.Text;
            string password = txtPassword.Text;
            bool login = false;          
            while (i < dt.Rows.Count)
            {
                DataRow drow = dt.Rows[i];
                dbUserName = drow[0].ToString();
                dbPassword = drow[1].ToString();
               
                string uName = dbUserName.Trim();
                string pw = dbPassword.Trim();
                if (username == uName && password == pw && role == index)
                {
                   
                    login = true;
                }
                i++;
                     
            }
            if (login == true)
            {
                  // Do your code
            }


这篇关于如何使用csharp中的combobox检查记录已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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