Click here to Skip to main content
15,884,298 members
Home / Discussions / Database
   

Database

 
GeneralRe: would you use RDLC? Pin
Mycroft Holmes20-Apr-14 12:54
professionalMycroft Holmes20-Apr-14 12:54 
GeneralRe: would you use RDLC? Pin
Jassim Rahma20-Apr-14 22:04
Jassim Rahma20-Apr-14 22:04 
QuestionOld school dbase II / clipper database, multiple left joins Pin
jkirkerx17-Apr-14 6:31
professionaljkirkerx17-Apr-14 6:31 
AnswerRe: Old school dbase II / clipper database, multiple left joins Pin
Jörgen Andersson17-Apr-14 10:38
professionalJörgen Andersson17-Apr-14 10:38 
General[SOLVED] Pin
jkirkerx17-Apr-14 10:49
professionaljkirkerx17-Apr-14 10:49 
GeneralRe: Old school dbase II / clipper database, multiple left joins Pin
jkirkerx17-Apr-14 11:03
professionaljkirkerx17-Apr-14 11:03 
QuestionSQL Query Pin
Syafiqah Zahirah17-Apr-14 6:00
Syafiqah Zahirah17-Apr-14 6:00 
AnswerRe: SQL Query Pin
Richard Deeming17-Apr-14 6:59
mveRichard Deeming17-Apr-14 6:59 
The first problem is that your code is susceptible to SQL Injection[^]. You should be using a parameterized query instead.

While we're fixing that, the SqlConnection, SqlCommand and SqlDataReader objects all implement IDisposable, so they should be wrapped in using blocks to make sure their resources are freed.

We'll also get rid of the SELECT *, since you only need five columns.

C#
string sql = "SELECT House, DateT, TempIn, tempOut, Humidity FROM Environmnets INNER JOIN TDevice ON Environmnets.Deviceid = TDevice.id INNER JOIN TMushroomHouse ON TDevice.MushroomHouseId = TMushroomHouse.id WHERE (Environmnets.DateT = (SELECT MAX(DateT) AS Expr1 FROM Environmnets AS Environmnets_1)) AND TMushroomHouse.Name = @MushroomHouseName";

using (SqlConnection con = new SqlConnection(strConnString))
using (SqlCommand cmd = new SqlCommand(sql, con))
{
    cmd.Parameters.AddWithValue("@MushroomHouseName", DropDownList1.Text);
    
    con.Open();
    
    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        while (dr.Read())
        {
            HouseTextBox.Text = dr["House"].ToString();
            DateTTextBox.Text = dr["DateT"].ToString();
            TempInTextBox.Text = dr["TempIn"].ToString();
            TempOutTextBox.Text = dr["tempOut"].ToString();
            HumidityTextBox.Text = dr["Humidity"].ToString();
        }
    }
}


Now, if the code is definitely executing, and there's no error, but you're not seeing anything in the textboxes, then that means there were no records returned by your query. If I had to guess, I'd say it's probably connected to the DateT filter:
SQL
WHERE (Environmnets.DateT = (SELECT MAX(DateT) AS Expr1 FROM Environmnets AS Environmnets_1))

I suspect you want the last record for a specific TMushroomHouse, but you're actually only looking for the last record overall, and only returning it if it matches the selected house.

If that's the case, try changing your query to:
SQL
SELECT TOP 1 House, DateT, TempIn, tempOut, Humidity FROM Environmnets INNER JOIN TDevice ON Environmnets.Deviceid = TDevice.id INNER JOIN TMushroomHouse ON TDevice.MushroomHouseId = TMushroomHouse.id WHERE TMushroomHouse.Name = @MushroomHouseName ORDER BY Environmnets.DateT DESC




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: SQL Query Pin
Syafiqah Zahirah17-Apr-14 20:18
Syafiqah Zahirah17-Apr-14 20:18 
GeneralRe: SQL Query Pin
Richard Deeming22-Apr-14 2:28
mveRichard Deeming22-Apr-14 2:28 
QuestionWhy connection is failing?! Pin
Jassim Rahma16-Apr-14 9:59
Jassim Rahma16-Apr-14 9:59 
QuestionRe: Why connection is failing?! Pin
Eddy Vluggen16-Apr-14 10:43
professionalEddy Vluggen16-Apr-14 10:43 
AnswerRe: Why connection is failing?! Pin
Jassim Rahma16-Apr-14 10:50
Jassim Rahma16-Apr-14 10:50 
GeneralRe: Why connection is failing?! Pin
Mycroft Holmes16-Apr-14 12:57
professionalMycroft Holmes16-Apr-14 12:57 
GeneralRe: Why connection is failing?! Pin
Jassim Rahma16-Apr-14 12:59
Jassim Rahma16-Apr-14 12:59 
GeneralRe: Why connection is failing?! Pin
Bernhard Hiller16-Apr-14 20:54
Bernhard Hiller16-Apr-14 20:54 
GeneralRe: Why connection is failing?! Pin
Jassim Rahma16-Apr-14 22:38
Jassim Rahma16-Apr-14 22:38 
GeneralRe: Why connection is failing?! Pin
Eddy Vluggen17-Apr-14 0:26
professionalEddy Vluggen17-Apr-14 0:26 
GeneralRe: Why connection is failing?! Pin
Jassim Rahma17-Apr-14 0:42
Jassim Rahma17-Apr-14 0:42 
GeneralRe: Why connection is failing?! Pin
Eddy Vluggen17-Apr-14 10:46
professionalEddy Vluggen17-Apr-14 10:46 
Questionwhy looping is not starting from my start date? Pin
Jassim Rahma16-Apr-14 0:04
Jassim Rahma16-Apr-14 0:04 
AnswerRe: why looping is not starting from my start date? Pin
jimbowAX16-Apr-14 3:04
jimbowAX16-Apr-14 3:04 
QuestionRe: why looping is not starting from my start date? Pin
Richard MacCutchan16-Apr-14 6:01
mveRichard MacCutchan16-Apr-14 6:01 
AnswerRe: why looping is not starting from my start date? Pin
Jassim Rahma16-Apr-14 6:14
Jassim Rahma16-Apr-14 6:14 
SuggestionRe: why looping is not starting from my start date? Pin
Richard MacCutchan16-Apr-14 6:29
mveRichard MacCutchan16-Apr-14 6:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.