为什么我的代码没有将数据保存到数据库并显示到datagridview? [英] Why my code in not saving the data into database and showing into datagridview?

查看:94
本文介绍了为什么我的代码没有将数据保存到数据库并显示到datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据投放文本框插入数据库,并在ok按钮单击时在datagridview中显示它。但是它不起作用也没有显示任何错误



贝娄是我试过的代码



< b>我尝试了什么:



I want to insert the data throw textboxes to database and show it in datagridview on ok button click. but it does not work nor show any error

bellow is the code that i have tried

What I have tried:

private void btn_ok_Click(object sender, EventArgs e)
{
    if (tb_itemCode.Text == "" || cb_itemName.Text == "")
    {
        System.Windows.Forms.MessageBox.Show("''Please Fill All Fileds'' ");
    }
    else
    {
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = @"insert into Invoices ([Item_Code], [Item_Name], ) values ('" + tb_itemCode.Text + "','" + cb_itemName.Text + "')";
        try
        {
            conection.Open();
            cmd.Connection = conection;
            cmd.ExecuteNonQuery();
            conection.Close();
            cleardata();

            OleDbDataAdapter DA = new OleDbDataAdapter("SELECT * FROM Invoices where Item_Code='" + tb_itemCode.Text + "' ", conection);
            DataTable DT = new DataTable();
            DA.Fill(DT);
            dgv_invoice.DataSource = null;
            dgv_invoice.DataSource = DT;
            dgv_invoice.Refresh();
        }
        catch (Exception exp)
        {
            MessageBox.Show(CommonFunction.CommonError);
        }
        finally
        {
            conection.Close();
        }
    }
}

推荐答案

首先应检查通话的返回值 SqlCommand.ExecuteNonQuery Method(System.Data) .SqlClient) [ ^ ],检查您的INSERT是否有效。
You should start by checking the return value of your call to SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient)[^], to check whether your INSERT has worked.


这篇关于为什么我的代码没有将数据保存到数据库并显示到datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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