Help in simple Registration System

Discussion in 'C#' started by DavCel, Dec 15, 2011.

  1. DavCel

    DavCel New Member

    Joined:
    Nov 23, 2010
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Part time C# programmer
    Location:
    Paco, Manila
    I am creating a simple registration system using microsoft visual studio 2008 in C# with SQL database 2008, where I can ADD user, EDIT user, & DELETE user. I already finished the DELETE button with a DELETE QUERY running, as well as the EDIT button with an UPDATE QUERY running and passing the value of the selected row in the DATAGRIDVIEW.

    My problem is about the ADD USER button, I created a FORM named "frm_add" to GET a value from the textbox and add it to the SQL database. I can open the form "frm_add" but when I finish putting some value and clicking the "add user" button the query doesn't work, it doesn't pass the value to the database here's my code

    FORM of the user management with the buttons of "add user", "edit user" & "delete user"
    (frm_UserManage)
    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.SqlClient;
    
    namespace RegistrationForm
    {
        public partial class frm_UserManage : Form
        {
            public static string server_ip = "localhost";
            public static string server_db = "db_regform";
            public static string server_user = "admin";
            public static string server_pass = "admin1";
    
            public static string ConnectionString = "Data Source=" + server_ip + ";" +
                                   "Initial Catalog=" + server_db + "; " +
                                   "User ID=" + server_user + ";" +
                                   "Password=" + server_pass + ";" +
                                   "Integrated security = false";
            
            SqlConnection myConnection = new SqlConnection(ConnectionString);
            
            public frm_UserManage()
            {
                InitializeComponent();
            }
    
            private void frm_UserManage_Load(object sender, EventArgs e)
            {
                /**SqlConnection myConnection = new SqlConnection(ConnectionString);
                myConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(sqlQuery, myConnection);
                da.SelectCommand.CommandType = CommandType.Text;
    
                DataTable dt = new DataTable();
                da.Fill(dt);
                dgview_regform.DataSource = dt;*/
                                     
    
            }
    
            private void btn_add_Click(object sender, EventArgs e)
            {
                try
                {
                   frm_add af = new frm_add();
                   af.ShowDialog();
                }
                catch (Exception)
                {
                    MessageBox.Show("Select row first!");
                }
            }
    
            private void btn_edit_Click(object sender, EventArgs e)
            {
                try
                {
                    frm_edit ef = new frm_edit(dgview_regform.CurrentRow.Cells[0].Value.ToString());
                    ef.ShowDialog();
                }
                catch (Exception)
                {
                    MessageBox.Show("Select row first!");
                }
                
            }
    
            private void btn_delete_Click(object sender, EventArgs e)
            {
                myConnection.Open();
                try
                {
                    string sqlQuery = "delete from tbl_regform where ID='" + dgview_regform.CurrentRow.Cells[0].Value.ToString() + "' ";
                    SqlCommand command = new SqlCommand(sqlQuery, myConnection);
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();
                    MessageBox.Show("Row Deleted!");                             
                }
                catch(Exception)
                {
                    MessageBox.Show("select row first!");
                }
                myConnection.Close();
            }
    
            private void btn_view_Click(object sender, EventArgs e)
            {
                myConnection.Open();
                string sqlQuery = "select * from tbl_regform";
    
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(sqlQuery, myConnection);
                da.SelectCommand.CommandType = CommandType.Text;
    
                DataTable dt = new DataTable();
                da.Fill(dt);
                dgview_regform.DataSource = dt;
                myConnection.Close();
            }
    
            private void btn_exit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }       
                   
        }
    }
    
    

    This code is for the form add user to get the value
    "frm_add"
    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.SqlClient;
    
    namespace RegistrationForm
    {
        public partial class frm_add : Form
        {
            public static string server_ip = "localhost";
            public static string server_db = "db_regform";
            public static string server_user = "admin";
            public static string server_pass = "admin1";
    
            public static string ConnectionString = "Data Source=" + server_ip + ";" +
                                   "Initial Catalog=" + server_db + "; " +
                                   "User ID=" + server_user + ";" +
                                   "Password=" + server_pass + ";" +
                                   "Integrated security = false";
    
            SqlConnection myConnection = new SqlConnection(ConnectionString);
    
            public frm_add()
            {
                InitializeComponent();
            }
    
            private void btn_adduser_Click(object sender, EventArgs e)
            {
                myConnection.Open();
                try
                {
                    string sqlQuery = "insert into tbl_regform (FirstName, LastName, Contact) values (FirstName='" + txtbx_fn.Text + "', LastName='" + txtbx_ln.Text + "', Contact='" + txtbx_con.Text + "') ";
                    SqlCommand command = new SqlCommand(sqlQuery, myConnection);
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();
                    MessageBox.Show("Data Added!");
    
                 }
                 catch (Exception ex)
                 {
                        MessageBox.Show(ex.Message);
                 }
    
                myConnection.Close();
            }
    
            private void frm_add_Load(object sender, EventArgs e)
            {
                
            }
            
    
        }
    }
    
    
    hope you can help me thanks..
     
  2. Resu

    Resu New Member

    Joined:
    Dec 11, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    i think your insert query is wrong. the values don't need to contain the fieldname
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice