繁体   English   中英

Datagridview和npgsql的Update命令出现问题

[英]Trouble with Update command for datagridview and npgsql

我在使用NpgsqlCommandBuilder和datagridview时遇到了一些问题。

用数据填充datagridview是没有问题的,但是我无法使updatecommand起作用。

我的代码看起来像这样

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Npgsql;

namespace Golfklubben
{
    public partial class LaggTillResultat : Form
    {
        private databaseConnection dataConnection = new databaseConnection();
        private BindingSource bindingSource1 = new BindingSource();
        private NpgsqlDataAdapter daResult = new NpgsqlDataAdapter();

        public LaggTillResultat()
        {
            InitializeComponent();
            FillTavlingar();
            dataGridView1.DataSource = bindingSource1;
            FillResultat();
            dataGridView1.Columns[0].HeaderText = "Deltagare";
            dataGridView1.Columns[1].HeaderText = "Poäng";
        }

        /// <summary>
        /// Metod för att fylla en combobox med tävlingar från databasen med en dataadapter 
        /// Method for filling a combobox with data from the database
        /// </summary>
        private void FillTavlingar()
        {
            DateTime date = DateTime.Now;
            string strDate = date.ToString("yyyy-MM-dd");
            try
            {
                DataTable table = new DataTable();
                NpgsqlConnection conn = new NpgsqlConnection(dataConnection.getConnection());
                NpgsqlDataAdapter da = new NpgsqlDataAdapter();
                NpgsqlCommand command = new NpgsqlCommand("SELECT tavlingsID, titel FROM tavlingar WHERE datum > @datum ORDER BY titel ASC;", conn);
                command.Parameters.AddWithValue("@datum", strDate);
                da.SelectCommand = command;

                da.Fill(table);
                cbTavling.DataSource = table.DefaultView;
                cbTavling.DisplayMember = "titel";
                cbTavling.ValueMember = "tavlingsID";
            }
            catch (Exception err)
            {
                MessageBox.Show("Ett fel uppstod när tävlingarna skulle hämtas " + err.ToString(), "Fel vid uppdatering av handicap", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        /// <summary>
        /// Metod för att fylla en datagridview med resultat från databasen
        /// Method for filling the datagridview with data from the datbase
        /// </summary>
        private void FillResultat()
        {
            if (cbTavling.SelectedValue == null)
            {
                MessageBox.Show("Det finns ingen tävling idag!\nDialogrutan kommer att stängas.");
                this.Close();
            }

            try
            {
                NpgsqlConnection conn = new NpgsqlConnection(dataConnection.getConnection());
                NpgsqlCommand command = new NpgsqlCommand("SELECT golfid, resultat FROM anmalningar WHERE tavlingsid = @tavlingsID ORDER BY golfid ASC;", conn);
                command.Parameters.AddWithValue("@TavlingsID", cbTavling.SelectedValue.ToString());

                daResult = new NpgsqlDataAdapter(command);

                NpgsqlCommandBuilder cb = new NpgsqlCommandBuilder(daResult);

                DataTable dt = new DataTable();
                dt.Locale = System.Globalization.CultureInfo.InvariantCulture;

                daResult.Fill(dt);
                bindingSource1.DataSource = dt;

            }
            catch (Exception err)
            {
                MessageBox.Show("FEL" + err.ToString());
            }

        }

        private void btnSpara_Click(object sender, EventArgs e)
        {

            try
            {
                daResult.Update((DataTable)bindingSource1.DataSource);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }

            FillResultat();
        }

        private void cbTavling_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillResultat();
        }

        /// <summary>
        /// Hanterar formclosing eventet som körs när formuläret håller på att stängas, gömmer formuläret istället för att stänga det.
        /// Det här är för att det annars inte går att öppna upp formuläret igen.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void btnAvbryt_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Vill du stänga formuläret?", "Stänger formuläret", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.Hide();
            }
            else
                return;
        }
    }


}

我使用了MSDN的本教程来尝试解决此问题。 MSDN

请使用conn.Open(); 在执行命令之前

例如

           DataTable table = new DataTable();
            NpgsqlConnection conn = new NpgsqlConnection(dataConnection.getConnection());
            conn.Open();
            NpgsqlDataAdapter da = new NpgsqlDataAdapter();
            NpgsqlCommand command = new NpgsqlCommand("SELECT tavlingsID, titel FROM tavlingar WHERE datum > @datum ORDER BY titel ASC;", conn);
            command.Parameters.AddWithValue("@datum", strDate);
            da.SelectCommand = command;

            da.Fill(table);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM