Hamood Shi1707
Hamood Shi1707

Reputation: 71

string must be exactly one character long issue

string SqlSelectQuery = " Select * From KTS Where STAFFNAME =" + Convert.ToChar(textBox1.Text);
        SqlCommand cmd = new SqlCommand(SqlSelectQuery, CON);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())

            textBox2.Text = (dr["JOB TITLE"].ToString());
            textBox3.Text = (dr["EXTN"].ToString());
            textBox4.Text = (dr["Direct No."].ToString());
            textBox5.Text = (dr["MOBILE (OFFICE)"].ToString());
            textBox6.Text = (dr["SNO"].ToString());

i want to load data from sql server to visual studio by entering the name of the first name for the employee and he's job title mobile ext ....blla blla blla appear in the textboxes and my error is string must be exactly one character long

Upvotes: 1

Views: 2957

Answers (2)

Stefano Zanini
Stefano Zanini

Reputation: 5926

You probably should use

Convert.ToString(textBox1.Text);

instead of

Convert.ToChar(textBox1.Text);

because you can't fit a String into a Char, and the textbox content will be most likely longer than one character

As per @RupeshPandey answer, you're also missing the quote to delimit the string in your query. your instruction should be

string SqlSelectQuery = "Select * From KTS Where STAFFNAME = '" +
                        Convert.ToString(textBox1.Text) +
                        "'";

Upvotes: 0

Rupesh Pandey
Rupesh Pandey

Reputation: 71

Convert.ToChar(textBox1.Text) requires a single character string, otherwise it throws a FormatException.

Your query should be

string SqlSelectQuery = " Select * From KTS Where STAFFNAME ='" + Convert.ToString(textBox1.Text)+"'";

Upvotes: 1

Related Questions