help please.. how can i store the selected value from my database to a variable in my c# form... (eg. variable=select idnum from tblename)<--- i know i cant do that.. how can i pass its value?? im talking about a specific value from the database. lets say i log in and the user name is "vegeta" how can i get its idnum and store it in a variable... thanks..

This post of yours has no much of a point.
"the selected value from my database"

There is no such thing as selected value in dataBase.

Please, be more specific of this issue of yours.

Edited 5 Years Ago by Mitja Bonca: n/a

This post of yours has no much of a point.
"the selected value from my database"

There is no such thing as selected value in dataBase.

Please, be more specific of this issue of yours.

in a queary statement in my c# form i want to select idnum of a user in the database , so the query should be SELECT idnum FROM tablename WHERE name="something". now what wanna do is when i clik a button that selected idnum will be stored in a variable in my form. thanks

int userID;

private void button_GetIdNum(object sender, EventArgs e)
{
    string userName = textBox1.Text; //this is an example, from where you get the name
    using(SqlConnection sqlConn = new SqlConnection("connString")) 
    {
         using(SqlCommand cmd = new SqlCommand())
         {
               cmd.CommandText = @"SELECT idnum FROM tablename WHERE name = @name";
               cmd.Parameters.Add("@name", SqlDbType.Int).Value = userName;
               cmd.Connection = sqlConn;               
               sqlConn.Open();
               using(SqlDataReader reader = cmd.ExecuteReader())
               {
                    if(reader.Read())
                    {
                        userID = (int)reader[0];  //now you have a form variable filled up
                        //and you can use it in a whole form (class) 
                        MessageBox.Show(String.Format("Id found. User with name {0} has id {1}.", userName, userID.ToString()));
                    }
                    else
                        MessageBox.Show("There is no user with this Id in the dataBase.");
               }
         }
    }
}

I'm having a similar problem. My table has a space in it and I get an error saying no object with the name etc is found. How would you rewrite

cmd.CommandText = @"SELECT idnum FROM tablename WHERE name = @name";

if tablename was changed to table name

Edited 5 Years Ago by MagnetoM: n/a

Space? What do you mean?
You mean the Field name (column name) has a white space, like:

Column Name

I would suggest you to remove spaces. Its not good to have them at all. There can occur problems.

Edited 5 Years Ago by Mitja Bonca: n/a

My tables that i need to read data from have spaces in their names. I don't think I can rename them but I'll try again later.

Depending on the database it is a possibility to include the name of the table in square brackets like [table name]. At least this works for SQL Server / Access, and if my memory is not cheating me, MySQL too... :-)

I'm using VS2010 with a sql database. I downloaded sql server 2008 r2 express. I'll try the square brackets once I get the chance due to hardware issues atm.

HI i have this code that when i log in the username will be in a variable. now, i wanna use that variable in other forms.. lets say that variable is in a FORM1 and i wanna use that variable from FORM2?

private void Lgbtn_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\Anakz\My Documents\PDG.mdb");
            OleDbCommand cmd = new OleDbCommand("SELECT Username,Password FROM Users", conn);
            OleDbDataReader reader;
            bool permit = false;
            string user;
            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    if (textBox1.Text == (reader["UserName"].ToString()) && textBox2.Text == reader["PassWord"].ToString())
                    {
                       
                       user = reader["UserName"].ToString(); //this where username from the database will pass its value to a variable..
                        permit = true;
                        break;
                    }
                    //conn.Close();
                }
            }



            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (permit == true)
            {
                this.Close();
                MessageBox.Show("access granted");
             }


            else
            { MessageBox.Show("access denied"); }
        }

:idea:

This article has been dead for over six months. Start a new discussion instead.