Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:28:04+00:00 2026-06-17T11:28:04+00:00

I have created a small software that connects to a small database. I am

  • 0

I have created a small software that connects to a small database. I am using C# and WinForms to connect to a local SQL server and display the database in a datagridview so far I have managed to successfully Add records to the database and select a record for editing.

So when I select a record for editing and I edit the wanted fields I then click the edit button which in theory should update the edited record. However when I do so I seem to face the following error:
(See Fig.1)

Fig.1enter image description here

I can not seem to work out why I this error is happening.

Interface: (fig.2)

Fig.2enter image description here

The code that carries out the editing btnEdit:

  private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                //Open Connection
                sc.Open();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM myEmployees WHERE EmpID" + 
                    txtID.Text + " ", sc);
                da.Fill(dt);

                //start the editing of the selected record
                dt.Rows[0].BeginEdit();

                dt.Rows[0][1] = txtFName.Text;
                dt.Rows[0][2] = txtLName.Text;
                dt.Rows[0][3] = txtJRole.Text;
                dt.Rows[0][4] = txtEmp.Text;
                //dt.Rows[0][1] =

                //stop editing
                dt.Rows[0].EndEdit();

                //sql commandbuilder that allow saving of records
                SqlCommandBuilder cb = new SqlCommandBuilder(da);

                //update the database
                da.Update(dt);

                //close connection
                sc.Close();

                loadEmp();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Application.ExitThread();
            }
        }

The datagridview cick event, this takes care of record selection for editing:

private void dgEmployees_Click(object sender, EventArgs e)
        {
            try
            {


                DataTable dt = new DataTable();
                SqlDataAdapter slctRow = new SqlDataAdapter("SELECT * FROM myEmployees WHERE EmpID=" +
                Convert.ToInt16(dgEmployees.SelectedRows[0].Cells[0].Value.ToString()) + " ", sc);
                slctRow.Fill(dt);

                //display records into textboxes
                txtID.Text = dt.Rows[0][0].ToString();
                txtFName.Text = dt.Rows[0][1].ToString();
                txtLName.Text = dt.Rows[0][2].ToString();
                txtJRole.Text = dt.Rows[0][3].ToString();
                txtEmp.Text = dt.Rows[0][4].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Application.ExitThread();
            }

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:28:05+00:00Added an answer on June 17, 2026 at 11:28 am

    Leaving aside issues of parameterization – you’ve missed an =:

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM myEmployees WHERE EmpID="+ 
                    txtID.Text + " ", sc); // WARNING: SQL INJECTION RISK
    

    However; I strongly suggest you to look at parameterization. For example, what happens if I type (into that text-box):

    0; delete from myEmployees --
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a small parser in c using flex and bison. The parser
I am using asp.net .. i have created one small application.. I have Configure
I have created small test web application which makes use of LINQ to SQL.
I have created a small C# class in a library. using System; using System.Collections.Generic;
I have created a small report using jasper reports and I previewed it using
We have a MVC3 application that we have created many small actions and views
We have a piece of software that attempts to print .tif images using a
I have created a small application in Xcode. How can i make that application
I have created an MSI package (Using InstallShield - IS) that installs my application
I have created a small chatting application in C#, and started as a Console

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.