Reputation: 317
I am trying to create a simple user registration form however at the line 29, myReader
is misbehaving and an app freezes there.
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 WindowsFormsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string myConnection = "datasource=s59.hekko.pl;port=3306;username=truex2_kuba;password=xxx";
string Query = "insert into truex2_kuba.users (uid,password) values('" + uid.Text + "','" + pwd.Text + "');";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Użytkownik Stworzony");
while (myReader.Read())
{
}
myConn.Close();
}
}
}
ERROR:
MySql.Data.MySqlClient.MySqlException was unhandled
ErrorCode=-2147467259
HResult=-2147467259
Message=Unknown column 'password' in 'field list'
Number=1054
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at WindowsFormsApplication1.Form5.button1_Click(Object sender, EventArgs e) in C:\Users\terleckk\Downloads\WindowsFormsApplication1 - Copy (1)\WindowsFormsApplication1 - Copy\WindowsFormsApplication1\Form5.cs:line 29
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in C:\Users\terleckk\Downloads\WindowsFormsApplication1 - Copy (1)\WindowsFormsApplication1 - Copy\WindowsFormsApplication1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Upvotes: 0
Views: 151
Reputation: 14389
since you are making an insert query you have to use executeNonQuery
instead of ExecuteReader
. Also use parameters:
try
{
string myConnection = "datasource=s59.hekko.pl;port=3306;username=truex2_kuba;password=xxx";
string Query = "insert into truex2_kuba.users (uid,password) values(@user,@pass);";
using (MySqlConnection myConn = new MySqlConnection(myConnection))
{
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
cmdDataBase.Parameters.AddWithValue("@user",uid.Text);
cmdDataBase.Parameters.AddWithValue("@pass",pwd.Text);
myConn.Open();
cmdDataBase.ExecuteNonQuery();
MessageBox.Show("Użytkownik Stworzony");
}
}
catch (SQlException ex)
{
MessageBox.Show(ex.Message);
}
Upvotes: 1
Reputation: 1118
The freezing could be caused because of the Database round-trip or some network issue. Alternatively, can you try to run the following code on a separate thread. This way your app will never freeze as the function below is run on separate thread and not on the main UI thread.
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 WindowsFormsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
Thread dbThread;
private void button1_Click(object sender, EventArgs e)
{
dbThread = new Thread(DbRead);
dbThread.Start ();
}
void DbRead()
{
string myConnection = "datasource=s59.hekko.pl;port=3306;username=truex2_kuba;password=xxx";
string Query = "insert into truex2_kuba.users (uid,password) values('" + uid.Text + "','" + pwd.Text + "');";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Użytkownik Stworzony");
while (myReader.Read())
{
}
myConn.Close();
}
}
}
Upvotes: 1