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

  • SEARCH
  • Home
  • 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 8740845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:12:11+00:00 2026-06-13T11:12:11+00:00

I’m trying to setup a simple C# form to search one of our SQL

  • 0

I’m trying to setup a simple C# form to search one of our SQL databases. I keep getting a ‘SQLException was Unhandled’ error when I try to debug. I know the SQL statement works, I can run it just fine on my server. I think it’s a problem with the way that I am trying to input the data from the TextBox. Can someone give me some guidance?

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 USER_Lookup
{
    public partial class Search : Form
    {
        DataSet ds = new DataSet();
        SqlConnection cs = new SqlConnection("Data Source=PC01; Initial Catalog=DB01; Integrated Security=TRUE");
        SqlDataAdapter da = new SqlDataAdapter();

        public Search()
        {
            InitializeComponent();
                    }

        private void button_Search_Click(object sender, EventArgs e)
        {
            string badgeCode = textBox_badgeCode.Text.Trim();
            da.SelectCommand = new SqlCommand
                ("SELECT db01.dbo.staff.lastname AS 'Last Name', db01.dbo.staff.firstname AS 'First Name', db01.dbo.staff.badgecode AS 'User ID', db01.dbo.staffrole.name AS 'Role' FROM db01.dbo.staff, db01.dbo.staffrole, db01.dbo.staff_staffrole WHERE db01.dbo.staff.badgecode =" + badgeCode + "AND db01.dbo.staff.id = db01.dbo.staff_staffrole.staff_id AND db01.dbo.staff_staffrole.staffrole_id = db01.dbo.staffrole.id", cs);
            ds.Clear();
            da.Fill(ds);

            dg.DataSource = ds.Tables[0];
        }
    }
}
  • 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-13T11:12:12+00:00Added an answer on June 13, 2026 at 11:12 am

    No space here

    ... db01.dbo.staff.badgecode =" + badgeCode + "AND db01.dbo.staff.id ....
                                                  ^^^^
    

    Also here you use maclive.dbo.staff.lastname but there is no FROM or JOIN for this database
    (typo?)

    And, if db01.dbo.staff.badgecode is a varchar field then you need single quotes around your badgeCode variable. However, string concatenation to form a SQL command is the sure path to Sql Injection attacks.
    Change your code to use parameters

    da.SelectCommand = new SqlCommand 
        ("SELECT maclive.dbo.staff.lastname AS 'Last Name', " + 
         "db01.dbo.staff.firstname AS 'First Name', db01.dbo.staff.badgecode AS 'User ID',  "+ 
         "db01.dbo.staffrole.name AS 'Role' " + 
         "FROM db01.dbo.staff, db01.dbo.staffrole, db01.dbo.staff_staffrole " + 
         "WHERE db01.dbo.staff.badgecode =@badge AND db01.dbo.staff.id = " + 
         "db01.dbo.staff_staffrole.staff_id AND db01.dbo.staff_staffrole.staffrole_id = " + 
         "db01.dbo.staffrole.id", cs);
    
    da.SelectCommand.Parameters.AddWithValue("@badge", badgecode);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.