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
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