如果表中不存在数据,则在自动完成文本框中插入数据 [英] Insert data on auto complete textbox if data not exist into table

查看:62
本文介绍了如果表中不存在数据,则在自动完成文本框中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的问题是如果自动完成文本框没有从数据库中返回任何值,如果插入详细信息意味着如果在自动文本框中找不到值,则弹出一个应该打开并告诉用户数据不可用你想要插入它,如果他点击是选项数据应该自动插入表中,但如果他下次搜索相同的名字他不会再问,实际上我不知道我该怎么做我写的代码在textchange活动但它不会


我尝试过:



hi all,

My question is How to insert detail if auto-complete textbox not return any value from database means if value not found in autotextbox one pop up should open and tell user data not available do you want to insert it if he click yes option data should automatically inserted into table but if he search next time same name he wont ask again, actually im not getting how should i do this i written code on textchange event but it wont

What I have tried:

string ab =(txtpartycode.Text);
string bc = txtpartycode1.Text;
con = new OleDbConnection(@" provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\Database\BillAndReceipt.accdb;Persist Security Info=False;");
con.Open();
string query = "Select Party_Code,Party_Code1 from Billing";
OleDbCommand cmd1 = new OleDbCommand(query, con);
OleDbDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
ab = dr[0].ToString();
bc = dr[1].ToString();
}
else
{
DialogResult dialogResult = MessageBox.Show("Data Not Found Do you want to create?", "Data insert Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dialogResult == DialogResult.Yes)
{
string SqlString = "Insert Into Billing (Party_Code,Party_Code1)Values (?,?)";
using (OleDbCommand cmd = new OleDbCommand(SqlString, con))
{
con.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Party_Code", txtpartycode.Text);
cmd.Parameters.AddWithValue("@Party_Code1", txtpartycode1.Text);
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Data Not inserted", "Data not Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
con.Close();

推荐答案

每次按键都会触发TextChanged事件,因此不是最佳选择。我会在验证事件上执行此操作



这是一个工作示例...请注意,我是当我填充集合时使用 AutoCompleteCustomSource - 我在FormLoad中为一个简单的文本框做了这个 txtNum1

The TextChanged event is fired for every keypress so isn't the best place to do this. I would do this on the Validating event

Here is a working example ... Note that I'm using AutoCompleteCustomSource when I populate the collection - I did this in the FormLoad for a simple textbox txtNum1
//Insert code here to read data into datatable dt
txtNum1.AutoCompleteMode = AutoCompleteMode.Suggest;
txtNum1.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtNum1.AutoCompleteCustomSource = new AutoCompleteStringCollection();
foreach (DataRow i in dt.Rows)
    txtNum1.AutoCompleteCustomSource.Add(i[0].ToString());



然后我可以使用


Then I can use

private void txtNum1_Validating(object sender, CancelEventArgs e)
{
    if (txtNum1.AutoCompleteCustomSource.Contains(txtNum1.Text) || string.IsNullOrEmpty(txtNum1.Text)) return;

    var msg = string.Format("Do you want to add {0} to the suggestion list?", txtNum1.Text);
    if (MessageBox.Show(msg, "Add text", MessageBoxButtons.YesNo) != DialogResult.Yes) return;

    txtNum1.AutoCompleteCustomSource.Add(txtNum1.Text);
    // Insert code here to update the database
}

(我颠倒了if语句以减少整个页面的嵌套)

(I inverted the if-statements to reduce the nesting across the page)


这篇关于如果表中不存在数据,则在自动完成文本框中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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