Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Data grid not populating

$
0
0

I am learning C# along with Visual Studio 2013. I finally was able to get all the errors cleared. But when my form comes up the data grid is empty. Even if I put in a search for a certain ID I get nothing.  

Here is my code: There is not a code block or am I in the wrong place?

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 System.Data.SQLite;

namespace StudentDB1
{
    public partial class Form1 : Form
    {

        private String connectionString;
        private SQLiteConnection connection;

        private String SQLInsert = "INSERT INTO User(LastName, FirstName, StuID, Grade, Address, City, State, Zip, Phone, ParentName) VALUES(?, ?)";
        private String SQLUpdate = "UPDATE User SET LastName = ?, FirstName = ?, StuID = ?, Grade = ?, Address = ?, City = ?, State = ?, Zip = ?, Phone = ?, ParentName = ? where UserId = ?";
        private String SQLSelect = "SELECT * FROM User";
        private String SQLDelete = "DELETE FROM User WHERE UserId = ?";



        public Form1()
        {
             InitializeComponent();
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
            connection = new SQLiteConnection(connectionString);
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

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

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();

            Application.Exit();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtStuId.Text))
            {
                if (connection.State != ConnectionState.Open)
                    connection.Open();

                SQLiteCommand command = connection.CreateCommand();
                command.CommandText = SQLInsert;

                command.Parameters.AddWithValue("FirstName", txtFirstName.Text);
                command.Parameters.AddWithValue("LastName", txtLastName.Text);

                command.ExecuteNonQuery();
                connection.Close();

           }
            else
            {
                 if (connection.State != ConnectionState.Open)
                    connection.Open();

                SQLiteCommand command = connection.CreateCommand();
                command.CommandText = SQLUpdate;

                command.Parameters.AddWithValue("FirstName", txtFirstName.Text);
                command.Parameters.AddWithValue("LastName", txtLastName.Text);
                command.Parameters.AddWithValue("UserId", int.Parse(txtStuId.Text));

                command.ExecuteNonQuery();
                connection.Close();

            clean();
            search();
          }
        }
         
        private void clean()
        {
            txtStuId.Text = String.Empty;
            txtFirstName.Text = String.Empty;
            txtLastName.Text = String.Empty;
            txtStuId.Text = String.Empty;
            txtGrade.Text = String.Empty;
            txtAddress.Text = String.Empty;
            txtCity.Text = String.Empty;
            txtState.Text = String.Empty;
            txtZip.Text = String.Empty;
            txtPhone.Text = String.Empty;
            txtParentName.Text = String.Empty;
        }

        private void search()
        {
            // Eliminamos el handler del evento RowEnter para evitar que se dispare al
            // realizar la búsqueda
            dataGrid.RowEnter -= dataGrid_RowEnter;

            // Abrimos la conexión
            if (connection.State != ConnectionState.Open)
                connection.Open();

            // Creamos un SQLiteCommand y le asignamos la cadena de consulta
            SQLiteCommand command = connection.CreateCommand();
            command.CommandText = SQLSelect;

            // Creamos un nuevo DataTable y un DataAdapter a partir de la SELECT.
            // A continuación, rellenamos la tabla con el DataAdapter
            DataTable dt = new DataTable();
            SQLiteDataAdapter da = new SQLiteDataAdapter(command);
            da.Fill(dt);

            // Asignamos el DataTable al DataGrid y cerramos la conexión
            dataGrid.DataSource = dt;
            connection.Close();

            // Restauramos el handler del evento
            dataGrid.RowEnter += dataGrid_RowEnter;
        }

        private void dataGrid_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // Recuperamos ID, nombre y apellido de la fila
            int id = int.Parse(dataGrid.Rows[e.RowIndex].Cells[0].Value.ToString());
            String name = (String)dataGrid.Rows[e.RowIndex].Cells[1].Value;
            String surname = (String)dataGrid.Rows[e.RowIndex].Cells[2].Value;

            // Asignamos los valores a las cajas de texto
            txtStuId.Text = id.ToString();
            txtFirstName.Text = name;
            txtLastName.Text = surname;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void txtId_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


Viewing all articles
Browse latest Browse all 2535

Trending Articles



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