Click here to Skip to main content
15,886,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I click the add button, nothing happens and also I do not get any errors.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Point_of_sale
{
    public partial class manageCustomers : Form
    {
        public manageCustomers()
        {
            InitializeComponent();
        }
        SqlConnection Conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\GARNET6\Documents\inventorydb.mdf;Integrated Security=True;Connect Timeout=30");
        void populate()
        {
            try
            {
                Conn.Open();
                string Myquerry = "select * from CustomerTb1";
                SqlDataAdapter da = new SqlDataAdapter(Myquerry, Conn);
                SqlCommandBuilder builder = new SqlCommandBuilder(da);
                var ds = new DataSet();
                da.Fill(ds);
                CustomersGV.DataSource = ds.Tables[0];
                Conn.Close();


            }
            catch
            {

            }
        }
        private void label6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
             
                Conn.Open();
                SqlCommand cmd = new SqlCommand("insert into CustomerTb1 values(" + CustomerId.Text + ", '" + CustomerNameTb.Text + "', '" + CustomerPhoneTb.Text + "', )", Conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Customer sucessfully added");
                Conn.Close();
                populate();

            }

            catch
            {
            }
        }
        

        private void CustomersGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void manageCustomers_Load_1(object sender, EventArgs e)
        {
            populate();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 20-Feb-22 23:18pm
Comments
Graeme_Grant 21-Feb-22 4:49am    
You need to name your objects. button3, label6 does not tell me what it does - context is key. Also, where is the code for the AddButton_Click?
bravian 21-Feb-22 5:02am    
hello, the button1 is the add button. Here is the code
private void button1_Click(object sender, EventArgs e)
{
try
{

Conn.Open();
SqlCommand cmd = new SqlCommand("insert into CustomerTb1 values(" + CustomerId.Text + ", '" + CustomerNameTb.Text + "', '" + CustomerPhoneTb.Text + "', )", Conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Customer sucessfully added");
Conn.Close();
populate();

}

catch
{
}
}
Graeme_Grant 21-Feb-22 5:20am    
did you set a breakpoint to: 1. see if the code executes; 2. step through the lines as they execute?

you have a try catch wrapper but the catch is empty, so you do not know if there was an error and what the error could be.

1 solution

Your datagrid will only update once you tell it to, same as what you did when the form loaded -
var ds = new DataSet();
                da.Fill(ds);
                CustomersGV.DataSource = ds.Tables[0];


Once you added new data, refresh the rid and your data will show the new record.

Make sure your added row was added in your table, meaning that your button_1code works fine.
 
Share this answer
 
Comments
bravian 21-Feb-22 5:43am    
so do I input this code within the button! code?
Andre Oosthuizen 21-Feb-22 5:51am    
Yes, after the update
bravian 21-Feb-22 6:11am    
sorry, It seems like I do not understand. Do you mean I should add It after populate?
Andre Oosthuizen 22-Feb-22 3:00am    
Yes, once you ave added a new record, you then need to refresh or re-load your datagrid.
bravian 25-Feb-22 6:20am    
once I use the code I get an error that the name does not exist in the current context

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900