Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am following the tutorial by How to Connect to MySQL Using C#[^]


For some reason I am getting the name
C#
dataGridView1 does not exists in the current context at the line 


dataGridView1.DataSource = ds.Tables[0].DefaultView;


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 MySql.Data.MySqlClient;



namespace connect4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //string MyConnectionString = "server=localhost;database=testdb;uid=root;pwd=;" ; 
        }

        private void button1_Click(object sender, EventArgs e)

        {

            string MyConnectionString = "server=localhost;database=testdb;uid=root;pwd=;" ; 

            //int MobileNo;
            //string Mobile = Mobile.Text;

            //int.TryParse(Mobile, out MobileNo);

            MySqlConnection con = new MySqlConnection(MyConnectionString);

            MySqlCommand cmd;

            con.Open();

            MessageBox.Show("connection open"); 


            try {

                cmd = con.CreateCommand();
                cmd.CommandText = "INSERT INTO  phonebook(Id, Name, MobileNo) VALUES (@ID, @Name,@MobileNo)";

                cmd.Parameters.AddWithValue("@Id", int.Parse(Id.Text));

                cmd.Parameters.AddWithValue("@Name", txtName.Text);

                cmd.Parameters.AddWithValue("@MobileNo", int.Parse(Mobile.Text));

                cmd.ExecuteNonQuery();

                MessageBox.Show("Values inserted"); 

            }

            catch (Exception)
            {

                throw;
                //MessageBox.Show("query failed"); 

            }


            finally
            {

                if (con.State == ConnectionState.Open)

                {
                    con.Close();

                    MessageBox.Show("connection closed");
                    LoadData();
                }
            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        
        private void LoadData()
        {

            string MyConnectionString = "server=localhost;database=testdb;uid=root;pwd=;"; 

            MySqlConnection con = new MySqlConnection(MyConnectionString);


            con.Open();

            MessageBox.Show("connection open");

            try
            {
                MySqlCommand cmd = con.CreateCommand();

                cmd.CommandText = "SELECT * FROM  phonebook";

                MySqlDataAdapter adap = new MySqlDataAdapter(cmd);

                DataSet ds = new DataSet();

                adap.Fill(ds);

                //bindingSource1.DataSource = ds;

                dataGridView1.DataSource = ds.Tables[0].DefaultView;


            }

            catch (Exception)
            {
                throw;
            }


            finally {


                if (con.State == ConnectionState.Open)
                {

                    con.Close();
                }
            
            
            
            }
            
        
        
        }


    }
}


What I have tried:

*------------------------------------------------------------------------------------------*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Posted
Updated 3-Nov-16 23:05pm
Comments
Suvendu Shekhar Giri 4-Nov-16 0:38am    
..and do you really have a datagridview on your form with named "dataGridView1"?

1 solution

ID and Mobile are not prefixed with txt (presumably the name of the control on the form.

What you should have done is:
open your database in the management tool (SSMS for sql server, not sure what for MySql)
Check that the record has been save correctly

Put a break point in the Button_Click event and stepped through the method, checking the values of the input.
 
Share this answer
 

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