Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Please help me here, I need to insert data from datagridview to ms access table, and I keep getting an error that says the insert into statement contains the following unknown field name : 'Username',please help me fix this.thanks

Please help me here, I need to insert data from datagridview to ms access table, and I keep getting an error that says the insert into statement contains the following unknown field name : 'Username',please help me fix this.thanks

Scheduled Pinned Locked Moved C#
helpcsharplinqgraphicsgame-dev
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    katlegoM
    wrote on last edited by
    #1

    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 System.Data.OleDb; namespace EmployeeAttendanceRegister { public partial class SignIN : Form { public SignIN() { InitializeComponent(); } public OleDbConnection GetsqlCon() { string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/game/Desktop/3rd year/2nd Semester/INYM 328/EmployeeAttendanceRegister/EmployeeAttendanceRegister/EmployeeAttendanceRegister.accdb"; OleDbConnection mycon = new OleDbConnection(connstring); mycon.Open(); return mycon; } public void getComm(string connstring1) { OleDbConnection sqlcon = this.GetsqlCon(); OleDbCommand sqlcomm = new OleDbCommand(connstring1, sqlcon); sqlcomm.ExecuteNonQuery(); sqlcomm.Dispose(); sqlcon.Close(); sqlcon.Dispose(); } private void button1_Click(object sender, EventArgs e) { try { string col1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col2 = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col3 = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col4 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col5 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col6 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col7 = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string insert_sql = "INSERT INTO Attendance(Username,Lastname,Firstname,Cellnumber,Officenumber,DepartmentName,Passwrd) VALUES('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "','" + col6 + "','" + col7 + "')"; this.getComm(insert_sql); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

    D L K 3 Replies Last reply
    0
    • K katlegoM

      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 System.Data.OleDb; namespace EmployeeAttendanceRegister { public partial class SignIN : Form { public SignIN() { InitializeComponent(); } public OleDbConnection GetsqlCon() { string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/game/Desktop/3rd year/2nd Semester/INYM 328/EmployeeAttendanceRegister/EmployeeAttendanceRegister/EmployeeAttendanceRegister.accdb"; OleDbConnection mycon = new OleDbConnection(connstring); mycon.Open(); return mycon; } public void getComm(string connstring1) { OleDbConnection sqlcon = this.GetsqlCon(); OleDbCommand sqlcomm = new OleDbCommand(connstring1, sqlcon); sqlcomm.ExecuteNonQuery(); sqlcomm.Dispose(); sqlcon.Close(); sqlcon.Dispose(); } private void button1_Click(object sender, EventArgs e) { try { string col1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col2 = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col3 = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col4 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col5 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col6 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col7 = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string insert_sql = "INSERT INTO Attendance(Username,Lastname,Firstname,Cellnumber,Officenumber,DepartmentName,Passwrd) VALUES('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "','" + col6 + "','" + col7 + "')"; this.getComm(insert_sql); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      First things first. Google for "SQL Injection Attack" to find out why building a SQL query string like you have is such a bad idea. I'll give you two hints: Security and Maintainability. Then Google for "C# Access Parameterized Query" for what to do about it.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        First things first. Google for "SQL Injection Attack" to find out why building a SQL query string like you have is such a bad idea. I'll give you two hints: Security and Maintainability. Then Google for "C# Access Parameterized Query" for what to do about it.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        K Offline
        K Offline
        katlegoM
        wrote on last edited by
        #3

        Alright thank you Dave I will.

        1 Reply Last reply
        0
        • K katlegoM

          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 System.Data.OleDb; namespace EmployeeAttendanceRegister { public partial class SignIN : Form { public SignIN() { InitializeComponent(); } public OleDbConnection GetsqlCon() { string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/game/Desktop/3rd year/2nd Semester/INYM 328/EmployeeAttendanceRegister/EmployeeAttendanceRegister/EmployeeAttendanceRegister.accdb"; OleDbConnection mycon = new OleDbConnection(connstring); mycon.Open(); return mycon; } public void getComm(string connstring1) { OleDbConnection sqlcon = this.GetsqlCon(); OleDbCommand sqlcomm = new OleDbCommand(connstring1, sqlcon); sqlcomm.ExecuteNonQuery(); sqlcomm.Dispose(); sqlcon.Close(); sqlcon.Dispose(); } private void button1_Click(object sender, EventArgs e) { try { string col1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col2 = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col3 = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col4 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col5 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col6 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col7 = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string insert_sql = "INSERT INTO Attendance(Username,Lastname,Firstname,Cellnumber,Officenumber,DepartmentName,Passwrd) VALUES('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "','" + col6 + "','" + col7 + "')"; this.getComm(insert_sql); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          "Username" is a reserved keyword[^], and a bad choice for a column-name. You'd have to escape it, probably by putting it in [username]. A better solution would be to rename the column. You'd also want to be using "parameterizes queries" to prevent people like me from deleting everything in there. And instead of manually disposing, I'd recommend using a using clause.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          D 1 Reply Last reply
          0
          • L Lost User

            "Username" is a reserved keyword[^], and a bad choice for a column-name. You'd have to escape it, probably by putting it in [username]. A better solution would be to rename the column. You'd also want to be using "parameterizes queries" to prevent people like me from deleting everything in there. And instead of manually disposing, I'd recommend using a using clause.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            D Offline
            D Offline
            Dholakiya Ankit
            wrote on last edited by
            #5

            ya i agree :)

            1 Reply Last reply
            0
            • K katlegoM

              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 System.Data.OleDb; namespace EmployeeAttendanceRegister { public partial class SignIN : Form { public SignIN() { InitializeComponent(); } public OleDbConnection GetsqlCon() { string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/game/Desktop/3rd year/2nd Semester/INYM 328/EmployeeAttendanceRegister/EmployeeAttendanceRegister/EmployeeAttendanceRegister.accdb"; OleDbConnection mycon = new OleDbConnection(connstring); mycon.Open(); return mycon; } public void getComm(string connstring1) { OleDbConnection sqlcon = this.GetsqlCon(); OleDbCommand sqlcomm = new OleDbCommand(connstring1, sqlcon); sqlcomm.ExecuteNonQuery(); sqlcomm.Dispose(); sqlcon.Close(); sqlcon.Dispose(); } private void button1_Click(object sender, EventArgs e) { try { string col1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col2 = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col3 = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col4 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col5 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col6 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string col7 = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString(); string insert_sql = "INSERT INTO Attendance(Username,Lastname,Firstname,Cellnumber,Officenumber,DepartmentName,Passwrd) VALUES('" + col1 + "','" + col2 + "','" + col3 + "','" + col4 + "','" + col5 + "','" + col6 + "','" + col7 + "')"; this.getComm(insert_sql); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

              K Offline
              K Offline
              katlegoM
              wrote on last edited by
              #6

              Thanks everyone for your responses,I managed to fix my code, now everything is fine. :-D

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups