Search code examples
c#winformssearchstored-procedurestextbox

Datagridview is not populating when submitting from winform search button


I currently have a C# winform that is compiling with zero errors and zero warnings:

This is my first go at a winform project:

Note: I have a similar project in ASPX that is working as expected

However this one needs to be in a winform.

I also need to start off with an empty datagrid because in the production environment the table we will be calling holds millions of rows.

(this cannot be a filter the datagrid type solution it would kill the server in production.)

the winform is super simple consisting of:

  • a form: GetParentID
  • a Text box: textBoxValueToSearch
  • a button: BTN_SEARCH
  • a data grid view: ParentIDOutput
  • a connection string in app.config : RCPDEV (tested and working)

Problem: when I enter a value in the text box and click on the search button I'm expecting a single Parent_Container_Id to be returned in the data grid view. However clicking on the button does nothing.

I have tested the stored procedure code and this is working properly:


IN PROGRAM.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace GetParentID
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new GetParentID());
        }
    }
}

IN GETPARENTID.DESIGNER.CS

namespace GetParentID
{
    partial class GetParentID
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBoxValueToSearch = new System.Windows.Forms.TextBox();
            this.BTN_SEARCH = new System.Windows.Forms.Button();
            this.ParentIDOutput = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.ParentIDOutput)).BeginInit();
            this.SuspendLayout();
            // 
            // textBoxValueToSearch
            // 
            this.textBoxValueToSearch.Location = new System.Drawing.Point(31, 22);
            this.textBoxValueToSearch.Name = "textBoxValueToSearch";
            this.textBoxValueToSearch.Size = new System.Drawing.Size(268, 20);
            this.textBoxValueToSearch.TabIndex = 0;
            // 
            // BTN_SEARCH
            // 
            this.BTN_SEARCH.Location = new System.Drawing.Point(305, 20);
            this.BTN_SEARCH.Name = "BTN_SEARCH";
            this.BTN_SEARCH.Size = new System.Drawing.Size(75, 23);
            this.BTN_SEARCH.TabIndex = 1;
            this.BTN_SEARCH.Text = "Search";
            this.BTN_SEARCH.UseVisualStyleBackColor = true;
            // 
            // ParentIDOutput
            // 
            this.ParentIDOutput.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.ParentIDOutput.Location = new System.Drawing.Point(31, 69);
            this.ParentIDOutput.Name = "ParentIDOutput";
            this.ParentIDOutput.Size = new System.Drawing.Size(349, 67);
            this.ParentIDOutput.TabIndex = 2;
            // 
            // GetParentID
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(408, 299);
            this.Controls.Add(this.ParentIDOutput);
            this.Controls.Add(this.BTN_SEARCH);
            this.Controls.Add(this.textBoxValueToSearch);
            this.Name = "GetParentID";
            this.Text = "GetParentID";
            ((System.ComponentModel.ISupportInitialize)(this.ParentIDOutput)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBoxValueToSearch;
        private System.Windows.Forms.Button BTN_SEARCH;
        private System.Windows.Forms.DataGridView ParentIDOutput;
    }
}

IN GETPARENTID.CS

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

namespace GetParentID
{
    public partial class GetParentID : Form
    {
        public GetParentID()
        {
            InitializeComponent();
        }
        BindingSource binder = new BindingSource();

        private void BTN_SEARCH_Click(object sender, EventArgs e)
        {

            // SqlConnection sqlCon = new SqlConnection(RCPDEV)
            string constr;

            string containerIdValue = textBoxValueToSearch.Text;

            constr = Properties.Settings.Default.RCPDEV;

            SqlConnection con = new SqlConnection(constr);

            con.Open();

            SqlCommand cmd = new SqlCommand("RFID_GET_CONTAINER_PARENT_ID", con);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@CONTAINER_ID", SqlDbType.NVarChar, 25).Value = containerIdValue;

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            DataTable dataTable = new DataTable();

            adapter.Fill(dataTable);

            ParentIDOutput.DataSource = binder;

            binder.DataSource = dataTable;

            con.Close();

            //FillDataGridView();
        }
    }
}

Any ideas on what the heck I am doing wrong here

/ i have been pulling my hair out for the last 16 hours or so trying many different things and reading whole lot of articles/tutorials/and help tickets to no avail.

I am at your mercy a to being able to fix this.


Solution

  • You should subscribe event like Control.Click to response any action by user on winform control.

    // BTN_SEARCH
    // 
    this.BTN_SEARCH.Click += BTN_SEARCH_Click;
    

    Or you can subscribe MouseClick.

    You should look into Control.Event list to find out what event fit into your situation.

    You can subscribe event on Properties Window instead write code.

    Event List