Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 10-27-2011, 11:23 AM   #1
Elite Ninja
Kawaii Desu
Difficulty Consulting Cat
Difficulty ConsultantFFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
Elite Ninja's Avatar
 
Join Date: Mar 2007
Age: 30
Posts: 4,811
Default C# programming help

In my programming class I am trying to make an SQL Database in Microsoft Visual C# 2010 Express using a windows form that would allow you to input data (such as a username and password) into the database.

This is the first time I have messed with SQL and I have no idea what I am doing >.> I am not sure what you would need to know to help me but I will provide the code I have and a SS of the table I have for the database.

Code:
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace SQL_Database_Project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void RegisterButton_Click(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserDatabase.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            string username = UsernameTextBox.Text;
            string password = PasswordTextBox.Text;
            string sqlquery = "INSTERT INTO [UserTable] (Username, Password) VALUES ('" + UsernameTextBox.Text + "," + "'" + PasswordTextBox.Text + "')";
            SqlCommand command = new SqlCommand(sqlquery, cn);
       
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }
            command.Parameters.AddWithValue("Username", username);
            command.Parameters.AddWithValue("Password", password);
            command.Parameters.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserDatabase.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);

            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }
        }
    }
}





__________________
Elite Ninja is offline   Reply With Quote
Old 10-27-2011, 11:38 AM   #2
Velocity
Doing the wrong thing the right way since 2010.
Site and Game Administrator
AdministratorRetired StaffDeveloperFFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
Velocity's Avatar
 
Join Date: Jul 2007
Posts: 1,812
Default Re: C# programming help

Code:
string sqlquery = "INSTERT INTO [UserTable] (Username, Password) VALUES ('" + UsernameTextBox.Text + "," + "'" + PasswordTextBox.Text + "')";
You spelt INSERT wrong.

I don't have any C# knowledge, just pointing the SQL mistype out :p

Last edited by Velocity; 10-27-2011 at 11:42 AM..
Velocity is offline   Reply With Quote
Old 10-27-2011, 11:47 AM   #3
Izzy
Snek
FFR Simfile AuthorFFR Veteran
 
Izzy's Avatar
 
Join Date: Jan 2003
Location: Kansas
Age: 33
Posts: 9,192
Default Re: C# programming help

I've never tried using SQL with C# but I don't see anything wrong with this simple example other than what velocity pointed out.

You have these lines both on load and when you click register. This is most likely redundant.

Code:
string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserDatabase.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
Perhaps just make sql connection CN a global undeclared variable, and declare it on load.
Izzy is offline   Reply With Quote
Old 10-27-2011, 11:52 AM   #4
Elite Ninja
Kawaii Desu
Difficulty Consulting Cat
Difficulty ConsultantFFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
Elite Ninja's Avatar
 
Join Date: Mar 2007
Age: 30
Posts: 4,811
Default Re: C# programming help

ok thanks. I will see if that fixes things. I feel so stupid not seeing that mistype x.x

I am in lunch right now and so I won't be able to try it until my next class which is LAN.
__________________
Elite Ninja is offline   Reply With Quote
Old 10-27-2011, 12:23 PM   #5
fido123
FFR Player
 
fido123's Avatar
 
Join Date: Sep 2005
Age: 32
Posts: 4,245
Default Re: C# programming help

Always check your database queries manually in the MySQL command line. Regardless of OS, get into its CLI and execute:
Code:
mysql -uroot -p
Assuming its on your local system and you don't have proper users set up.

Also if they're making you code applications that use databases with zero knowledge of them learn them. It's mind boggling to me why on earth they would do that.

Edit: also instead of taking a screen shot in the MySQL CLI you can just use: DESC <tableName>
then copy paste.

Last edited by fido123; 10-27-2011 at 02:38 PM..
fido123 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 04:25 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution