I have installed MySQL on my local computer and am trying to enable insertion of data to a created MySQL table from my ASP.NET web application on Visual Studio 2017. I've managed to set up a MySQL connection successfully and created table called 'events'.
Initially, I do not have any syntax or system errors, using the code below, but whenever I try to insert data from my web form upon clicking, no data is inserted into the MySQL table. Any advice if there is anything wrong in my code or any other file configurations?
Later, after I tried installing Connector for .NET, I still cannot insert data into the MySql and I get the below errors in namespace MySqlConnection stating 'the-type-mysqlconnection-exists-in-both-mysql-data-issue' as seen below
Any advice what can be corrected to be able to insert data into my table? Is it in my Insert code or does problem lie elsewhere? Below are my code:
AddEvent.aspx.cs:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace Prototype3
{
public partial class AddEvent : System.Web.UI.Page
{
//convert event date input into DateTime format on back-end of page
string dateStr = dateinput1.Value;
DateTime date;
//Saving data on added event into MySQL database
string constring = "server=localhost;user id=***********;
password=***********!;
persistsecurityinfo=True;database=homesecretary";
//Connection to database below-check!!
string Query = "INSERT into events (eventTitle) VALUES
('"+tb_eventTitle.Text+"')"; //Parse date input from jQuery
datepicker
MySqlConnection connDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, connDataBase);
MySqlDataReader myReader;
try
{
connDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
connDataBase.Close();
}
Web.config:
<configuration>
<connectionStrings>
<add name="homesecretaryConnectionString"
connectionString="server=localhost;user
id=********; password=********; persistsecurityinfo=True;
database=homesecretary" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
.....
</configuration>
usingblock. Then you won't need to explicitly Close it, since exiting the block calls Dispose which calls Close. Avoid string concatenation to construct queries or the code becomes vulnerable to SQL injection attacks: use SQL parameters.System.Configurationand use ConfigurationManager.ConnectionStrings.