none
Get Data from Database and Assign to Variable?

    Question

  • Hello guys..

    I know it's probably easy but I'm stuck at a point here.. On my ASP.Net project, I need to get only a single value from one table and assign to table. The logic is basically to change the theme of the page, like;

    if(variable = "blabla")
    {
       theme.set = "xxxyyy";
    }

     

    That's the logic i'm trying to use.

    The table I'm using has 3 fields: ThemeID, SelectedTheme, UserID. The query I'm using is:

     

    SELECT SelectedTheme FROM Themes WHERE UserID = 38
    
    --UserID = 38 is symbolic here...
    

     

    Now, the code behind is something like this, but I only get some numbers in return, but I need the VALUE of the specified query above....

     

    #region Global Variables
    //...
    string result;
    //...
    #endregion
    
    protected override void OnPreInit(EventArgs e)
        {
          SqlConnection cnn = new SqlConnection("server=.;database=SIMSv2;integrated security=true");
          cnn.Open();
    
          SqlDataAdapter da = new SqlDataAdapter("SELECT SelectedTheme FROM Themes WHERE UserID = 38", cnn);
    
          DataSet ds = new DataSet("SIMSv2");
    
          result = da.Fill(ds, "SelectedTheme").ToString();
          
    
          cnn.Close();
          base.OnPreInit(e);
        }
    

    What am i doing wrong??

     

    Thanks...


    Blitzkrieg
    Saturday, May 07, 2011 10:05 AM

Answers

  • Try to use SqlReader as explained in the below article

    http://lamahashim.blogspot.com/2010/04/c-read-insert-update-delete-from-sql.html


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by Lord Sedna Saturday, May 07, 2011 6:53 PM
    Saturday, May 07, 2011 10:18 AM
  • As AmaL explained, why would you use a whole DataSet (or one DataTable) to populate one textBox, or more of them? If you want to get data to poulate some textBoxes from one dataBase`s column (in your case: SelectedTheme). Its pointless. You better use SqlDataReader object. 

    string mySelectQuery = "SELECT SelectedTheme FROM Themes WHERE UserID = 38";
     SqlConnection myConnection = new SqlConnection(myConnString);
     SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
     myConnection.Open();
     SqlDataReader myReader = myCommand.ExecuteReader();
     string variable = null;
     if (myReader.Read())
     {
        variable = (string)reader[0];
     }
     myReader.Close();
     myConnection.Close();
    
     if(variable.Equals("blabla"))
     {
         //do you code in here
     }
    


    Mitja
    • Marked as answer by Lord Sedna Saturday, May 07, 2011 6:53 PM
    Saturday, May 07, 2011 12:23 PM

All replies

  • Try to use SqlReader as explained in the below article

    http://lamahashim.blogspot.com/2010/04/c-read-insert-update-delete-from-sql.html


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by Lord Sedna Saturday, May 07, 2011 6:53 PM
    Saturday, May 07, 2011 10:18 AM
  • As AmaL explained, why would you use a whole DataSet (or one DataTable) to populate one textBox, or more of them? If you want to get data to poulate some textBoxes from one dataBase`s column (in your case: SelectedTheme). Its pointless. You better use SqlDataReader object. 

    string mySelectQuery = "SELECT SelectedTheme FROM Themes WHERE UserID = 38";
     SqlConnection myConnection = new SqlConnection(myConnString);
     SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
     myConnection.Open();
     SqlDataReader myReader = myCommand.ExecuteReader();
     string variable = null;
     if (myReader.Read())
     {
        variable = (string)reader[0];
     }
     myReader.Close();
     myConnection.Close();
    
     if(variable.Equals("blabla"))
     {
         //do you code in here
     }
    


    Mitja
    • Marked as answer by Lord Sedna Saturday, May 07, 2011 6:53 PM
    Saturday, May 07, 2011 12:23 PM