Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace Project1
{
    public partial class Register : Form
    {
       private MySqlConnection myCon = new MySqlConnection("server=MKHX112208;uid=ojt;pwd=Password1;database=ask_it");
       private MySqlCommand myCom;
       

        public Register()
        {
            InitializeComponent();
        }

        MySqlConnection con;
        MySqlCommand cmd;
        MySqlDataReader dr;
       

        private void btnRegister_Click(object sender, EventArgs e)

        {
            con.Open();
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = con;
          

            {
                cmd.CommandText = "INSERT INTO users (firstname, lastname, username, password, emailadd) VALUES(@firstname, @lastname, @username, @password, @emailadd)";

                cmd.Parameters.AddWithValue("@firstname", txtFirstName.Text);
                cmd.Parameters.AddWithValue("@lastname", txtLastName.Text);
                cmd.Parameters.AddWithValue("@username", txtUserName.Text);
                cmd.Parameters.AddWithValue("@password", txtPassword.Text);
                cmd.Parameters.AddWithValue("@emailadd", txtEmailadd.Text);

               
                cmd.ExecuteNonQuery();
                con.Close();

                MessageBox.Show("Saving is done!");

            }
            
        }
Posted
Updated 5-Aug-14 17:02pm
v8

Just as the message says, you left the parentheses () off the previous statement, the call to the constructor


Also, you don't need to specify the datatype for the parameters.
And you may want to be consistent in using the @ .



You now have two connections and two commands.
The connection you are using has no connection string.
 
Share this answer
 
v4
Comments
Member 10994115 5-Aug-14 22:09pm    
So what should I do? Sorry i'm so new here with C#
PIEBALDconsult 5-Aug-14 22:23pm    
Add the parentheses where they belong.
Member 10994115 5-Aug-14 22:27pm    
still nothing changes. Same error. Red underlined cmd of cmd.connection = con;
PIEBALDconsult 5-Aug-14 22:29pm    
You'll also want a semi-colon. Can you update/improve the question with what you have now?
Member 10994115 5-Aug-14 22:41pm    
Done!
private void btnRegister_Click(object sender, EventArgs e)
{

Login.myCon.Open();
MySqlCommand cmd = new MySqlCommand("INSERT INTO users (firstname, lastname, username, password, emailadd) VALUES('"+ txtFirstName.Text +"', '"+ txtLastName.Text +"', '"+ txtUserName.Text +"', '"+ txtPassword.Text +"', '"+ txtEmailadd.Text +"')",Login.myCon);
cmd.ExecuteNonQuery();
Login.myCon.Close();

MessageBox.Show("Saving is done!");
this.Hide();

Login log = new Login();
log.Show();


This is the correct answer!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900