hello, I am building a interface that allow user to input or search a subject name.
SubjectName
Accounting
Biology
Bussiness
Computer Science
Computing
English
Law
Maths
The problem is if I tried to input a new subjectName, there will be a error showed
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' on program.cs
and another question is how to insert the new subject name to database, do I achieve it on btnNext button? how can textbox search a name from the database or insert a new subjectname
Any ideas? Thank you
What I have tried:
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 ProjectInterface
{
public partial class Concept : Form
{
MySqlConnection con = new MySqlConnection("server=localhost;Database=databse;Port = 3306; User ID = root; Password = password123");
MySqlCommand cmd = new MySqlCommand();
public Concept()
{
InitializeComponent();
}
private void btnNextC_Click(object sender, EventArgs e)
{
con.Open();
string newcon = "Insert into tutorials(TutorialName) VALUES ('" + txtTuName.Text + " ')";
MySqlCommand cmd = new MySqlCommand(newcon, con);
cmd.ExecuteNonQuery();
this.Hide();
Home home = new Home();
home.Show();
}
private void btnCancelC_Click(object sender, EventArgs e)
{
this.Hide();
Home home = new Home();
home.Show();
}
private void Concept_Load(object sender, EventArgs e)
{
}
private void txtSubject_TextChanged(object sender, EventArgs e)
{
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT SubjectName From databse.subject WHERE SubjectName LIKE @name",con);
cmd.Parameters.Add(new MySqlParameter("@name", "%" + txtSubject.Text + "%"));
MySqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection subjectColl = new AutoCompleteStringCollection();
while (dr.Read()) {
subjectColl.Add(dr.GetString(0));
}
txtSubject.AutoCompleteCustomSource = subjectColl;
con.Close();
}
}