TensE
TensE

Reputation: 49

C# SQL Search result into a string/textbox

I'm trying to create a registration form. I want to make a verification check where if ID in textbox already exists it would show an error msg. However whenever I try to search for ID and put it into a string I get an error

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll

here's my code:

public SignUpForm()
    {
        InitializeComponent();
    }
    // Connection String
    SqlCeConnection connect = new SqlCeConnection("Data Source= Accounts.sdf;Persist Security Info=false;");   

    private void btnRegister_Click(object sender, EventArgs e) {
        String verifyID = "";

        connect.Open();

        using (SqlCeCommand cmd = new SqlCeCommand("SELECT Student ID FROM Users WHERE Student ID = @ID", connect)) 
        {
            cmd.Parameters.AddWithValue("@ID", txtID.Text);
            using (SqlCeDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    verifyID = (string)reader["Student ID"];
                }
            }
        }

        txtTEMP.Text = verifyID;

Upvotes: 0

Views: 116

Answers (2)

Saagar Elias Jacky
Saagar Elias Jacky

Reputation: 2688

Your SQL SELECT [Student ID] FROM Users WHERE Student ID = @ID doesn't seems to be right. it should be

SELECT [Student ID] FROM Users WHERE [Student ID] = @ID

Upvotes: 2

JustAPup
JustAPup

Reputation: 1780

Maybe the error is in your SQL statement. Consider changing it to this:

SELECT StudentID FROM Users WHERE StudentID = @ID

Upvotes: 0

Related Questions