maddyrafi

New member
Joined
Mar 19, 2022
Messages
4
Programming Experience
Beginner
I want to insert table in db and make connection to mysql to vs. The error i attached in the attachment and coding given below

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

private void button6_Click(object sender, EventArgs e)
{
string id = textBox6.Text;
string name = textBox7.Text;
string salary = textBox8.Text;
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
string query = "INSERT INTO employee VALUES(@employee_id, @employee_name, @employee_salary)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@employee_id", id);
cmd.Parameters.AddWithValue("@employee_name", name);
cmd.Parameters.AddWithValue("@employee_salary", salary);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}

Error for above code is:
System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.


private void button8_Click(object sender, EventArgs e)
{
{
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source=LAPTOP-NDOAR92R;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new SqlConnection(connetionString);
cnn.Open();
MessageBox.Show("Connection Open!");
cnn.Close();
}
}

The Error for above code is:

Given in the attachment
 

Attachments

  • image.png
    image.png
    238.6 KB · Views: 99
  • image (1).png
    image (1).png
    315.9 KB · Views: 100
This line:
C#:
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
implies that you have an App.config file with an item named "constr" in the ConnectionStrings section. If that is is not the case then of course no such connection string will be found and you'll be trying to get the ConnectionString property of an object that doesn't exist, which will throw a NullReferenceException. If that's not where you're storing your connection string or that's not the name you're using to store it then that code won't work.
 
And as for the second error, your title says that you are trying to connect to MySql, but your code shows that you are trying to connect to SQL Server. The former requires that you use the MySqlConnection and MySqlCommand classes, but you are using the SqlConnection and SqlCommand classes.

Your connection string also looks like a SQL Server connection string rather than a MySQL connection string.

If you are really trying to connect to SQL Server (instead of MySQL), then verify that your anti-virus/firewall is setup to let access in through MSSQL's default ports, and that you actually have MSSQL running on that machine.
 
I wonder how this issue can be marked "Resolved" when the OP never bothered to acknowledge the answers he got ? Or did an admin mark it resolved just because of that ? Either way, asking for help and then remaining silent when you get it seems like seriously bad manners to me...
 
Back
Top Bottom