Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How can I solve "no elements" to update tables Using DataGridView

$
0
0

(Using DataGridView)

I tried to make update button after select name of customer ,then,

the form of Insert Customers appears with his details to make changes.
The form of insert customer its the same form to make changes.
but in in insert just adding.
and in update just seeing data that already exist to update it and save changes.

my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataAccess;
using System.Data;
using System.Data.SqlClient;

namespace Presentation
{
    public partial class FrInsertCus : Form

    {
        int customerID;
        bool update = false;
        Entities test4;
        public FrInsertCus()
        {                                   //this to insert
            InitializeComponent();
            test4=new Entities();
        }

        public FrInsertCus(Customers customer)
        {                                   // this to update
            InitializeComponent();
            customerID = customer.ID;
           test4 = new Entities();
            this.textBox1.Text = customer.Name;
            this.textBox2.Text = customer.Place_Of_Brith;
            this.dateTimePicker1.MinDate = customer.Date_Of_Brith;
            this.textBox3.Text = customer.Phone;
         
            update = true;
          
        }

        private void Insertbt(object sender, EventArgs e)
        {
            if (update == false)

                // try
                //{
            {
                Customers cus = new Customers();
                cus.Name = textBox1.Text;
                cus.Place_Of_Brith = textBox2.Text;
                cus.Date_Of_Brith = dateTimePicker1.MinDate;
                cus.Address = textBox3.Text;
                cus.Phone = textBox4.Text;
                test4.Customers.Add(cus);
                test4.SaveChanges();
                MessageBox.Show(@"Insert the Customer Successfully Completed ");
            }
            else
            {
                Customers cus = test4.Customers.First(x => x.ID == customerID);
                cus.Name = textBox1.Text;
                cus.Place_Of_Brith = textBox2.Text;
                cus.Date_Of_Brith = dateTimePicker1.Value;
                cus.Address = textBox3.Text;
                cus.Phone = textBox4.Text;
                test4.SaveChanges();
            }   
           // }
          //  catch (Exception ex )
          //  {
          //      MessageBox.Show(ex.Message,"Erorr",MessageBoxButtons.OK,MessageBoxIcon.Error);


          //  }
           

        }

 this code of update button in customer form.

private void update_but(object sender, EventArgs e)
        {

            string selectedrows = dataGridView1.SelectedCells.ToString();
            DataAccess.Customers customer= test1.Customers.First(x => x.Name == selectedrows);
            FrInsertCus cust=new FrInsertCus(customer);
            cust.Show();
}

I did not get any result.

this error

"sequence contains no elements"

How can I solve my code?








Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>