C
C#9mo ago
Mekasu0124

✅ ✅ Using SQLiteDataReader to return one item

public static CodeSession GetCodeSession(int? selectedId)
{
using SQLiteConnection conn = new SQLiteConnection(dbFile);
using SQLiteCommand cmd = new SQLiteCommand(conn);

SQLiteDataReader reader;

conn.Open();
cmd.CommandText = "SELECT * FROM logs WHERE Id=$selectedId";
cmd.Parameters.AddWithValue("$selectedId", selectedId);

reader = cmd.ExecuteReader();

CodeSession sess;

while(reader.Read())
{
int id = int.Parse(reader["Id"].ToString());
string date = reader["Date"].ToString();
string startTime = reader["StartTime"].ToString();
string endTime = reader["EndTime"].ToString();
string duration = reader["Duration"].ToString();

sess = new()
{
Id = id,
TodaysDate = date,
StartTime = startTime,
EndTime = endTime,
Duration = duration
};

return sess;
}
}
public static CodeSession GetCodeSession(int? selectedId)
{
using SQLiteConnection conn = new SQLiteConnection(dbFile);
using SQLiteCommand cmd = new SQLiteCommand(conn);

SQLiteDataReader reader;

conn.Open();
cmd.CommandText = "SELECT * FROM logs WHERE Id=$selectedId";
cmd.Parameters.AddWithValue("$selectedId", selectedId);

reader = cmd.ExecuteReader();

CodeSession sess;

while(reader.Read())
{
int id = int.Parse(reader["Id"].ToString());
string date = reader["Date"].ToString();
string startTime = reader["StartTime"].ToString();
string endTime = reader["EndTime"].ToString();
string duration = reader["Duration"].ToString();

sess = new()
{
Id = id,
TodaysDate = date,
StartTime = startTime,
EndTime = endTime,
Duration = duration
};

return sess;
}
}
I'm wanting to return just one item that is pulled from the database by it's ID. The only way I know how to use reader.Read() is inside of a while-loop and have it iterate through all items selected from the database table and then added to a list and returning the list, but I don't know how to use the reader.Read() to just return one item. Thanks
7 Replies
Saber
Saber9mo ago
don't put it in a loop and just call Read once before constructing the object?
Mekasu0124
Mekasu01249mo ago
I found a stack overflow that showed it used in an if statement and so far that's working. Thank you 🙂 I have another question if that's alright
Mekasu0124
Mekasu01249mo ago
When I put in 18:53 for 6:53PM, it kicks this error. This is supposed to calculate the difference in time stamps. So if I put in 18:53 for the start time and 19:33 for the end time, it's supposed to give back the difference between those two times, however, it's saying that input isn't in the correct format for 18:33. What do I have wrong?
No description
MODiX
MODiX9mo ago
Mekasu0124
REPL Result: Failure
public static string? IsValidTimeFormat(string input)
{
TimeSpan dummyOutput;
while (!TimeSpan.TryParse(input, out dummyOutput))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Invalid DateTime Format.");
Console.WriteLine("Use HH:MM Format");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Your Entry: ");

input = Console.ReadLine();
return IsValidTimeFormat(input);
}

return dummyOutput.ToString("HH:MM");
}

IsValidTimeFormat("18:53");
public static string? IsValidTimeFormat(string input)
{
TimeSpan dummyOutput;
while (!TimeSpan.TryParse(input, out dummyOutput))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Invalid DateTime Format.");
Console.WriteLine("Use HH:MM Format");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Your Entry: ");

input = Console.ReadLine();
return IsValidTimeFormat(input);
}

return dummyOutput.ToString("HH:MM");
}

IsValidTimeFormat("18:53");
Exception: FormatException
- Input string was not in a correct format.
- Input string was not in a correct format.
Compile: 701.471ms | Execution: 43.111ms | React with ❌ to remove this embed.
Mekasu0124
Mekasu01249mo ago
!e
public static string? IsValidTimeFormat(string input)
{
TimeSpan dummyOutput;
while (!TimeSpan.TryParse(input, out dummyOutput))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Invalid DateTime Format.");
Console.WriteLine("Use HH:MM Format");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Your Entry: ");

input = Console.ReadLine();
return IsValidTimeFormat(input);
}

return dummyOutput.ToString("HH:MM");
}

IsValidTimeFormat("18:53 PM");
public static string? IsValidTimeFormat(string input)
{
TimeSpan dummyOutput;
while (!TimeSpan.TryParse(input, out dummyOutput))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Error] Invalid DateTime Format.");
Console.WriteLine("Use HH:MM Format");

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Your Entry: ");

input = Console.ReadLine();
return IsValidTimeFormat(input);
}

return dummyOutput.ToString("HH:MM");
}

IsValidTimeFormat("18:53 PM");
MODiX
MODiX9mo ago
Mekasu0124
REPL Error
An error occurred while sending a request to the REPL service. This may be due to a StackOverflowException or exceeding the 30 second timeout. Details: An error occurred while sending the request.
Tried to execute
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts