Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Connection string Error C# to Mysql insert button code

madyrafi

New Coder
I want to insert table in db and make connection to mysql to vs.

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

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

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 below code is:

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'

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();
}
}
 

Attachments

  • image (1).png
    image (1).png
    315.9 KB · Views: 6
  • image.png
    image.png
    238.6 KB · Views: 6
Last edited by a moderator:

New Threads

Buy us a coffee!

Back
Top Bottom