Click here to Skip to main content
13,055,047 members (74,465 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I wanted to store the result of a mysql query into a string variable in C#. I tried using the following code but it is not working. Can anyone please suggest a solution.
MySqlCommand comm = new MySqlCommand("SELECT Employee_Name FROM server_side_user WHERE Employee_Username="+ "'" + currentUser + "'" + "", conn);           
MySqlDataReader currentLoggedInUser = comm.ExecuteReader();
while (currentLoggedInUser.Read())
    string user = currentLoggedInUser.GetString(0);

When I run the above code, the variable currentUsername will contain the value false. Can anybody suggest a proper code?

I want to store the result of the query into the variable user.
Posted 25-Feb-13 2:53am
Updated 25-Feb-13 3:03am
Richard MacCutchan 25-Feb-13 9:19am
It looks like your SELECT statement did not return any rows, so the first value returned is zero (or false). Check the actual content of your command and the values returned.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

As said by OriginalGriff above, you should always use parametrized query to avoid SQL Injection.

And if you know your query will return single value, you should use ExecuteScalar method instead of ExecuteReader.

string EmployeeName=comm.ExecuteScalar().ToString();
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

try like this.... ..

Query("SELECT SaksNummer FROM casetracking")
    public static string Query(string query)
        string x;
        cmd = new MySqlCommand(query, mysqlCon);
        x = cmd.ExecuteScalar().ToString();
        return x;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

First off, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Second, if your query worked, then you will load the last of any returned values into your "user" variable (because it is in a loop and the last value will overwrite all the others) - and then you will throw it away without using it because "user" goes out of scope at the end of the loop.

At no point do you modify or even reference a variable called currentUsername so it will have the same value at the end as it did at the start...

I think you need to sit down and think about what you are tryingto achieve here - because this looks a lot like either very muddled thinking, or the old "chuck it together and hope it works by magic" approach. Neither of these are a good idea...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Try This ->
 static string Put_In_String()
            SqlConnection myConnection = new SqlConnection();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select Query";
			var result = cmd.ExecuteReader();
			var myArray = new string[,] {};
			var counter = 0;
			while (result.Read())
				myArray[counter, 0] = (string) result["First Column Name"];
				myArray[counter, 1] = (string) result["Second Column Name"];
				// etc...
            return myArray ;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100