C
C#3w ago
Silme94

✅ Database cmd.ExecuteScalar() ERROR

Im trying to import a database to another
No description
13 Replies
Silme94
Silme943w ago
No description
Silme94
Silme943w ago
this part
viceroypenguin
Can you share a full code repository?
Silme94
Silme943w ago
ok
Silme94
Silme943w ago
GitHub
GitHub - Silme94/project
Contribute to Silme94/project development by creating an account on GitHub.
viceroypenguin
@Silme94 order of operations: * line 463: connection.Open() * line 476: InsertThematic(), which both opens and closes connection * line 477: attempts to use the connection that was closed inside of InsertThematic()
Silme94
Silme943w ago
alr
viceroypenguin
also, the fact that you close the connection inside of InsertThematic() means that when you reopen it, the last_insert_rowid() is no longer relevant. that valud is only valid for a single open session. closing the connection means that information is lost and can't be accessed by opening a new connection to the db. you might update InsertThematic to return an int, and inside of InsertThematic(), before closign the connection, do that GetLastInsertId()
private int ExecuteNonQueryWithId(string query, params SQLiteParameter[] parameters)
{
try
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
}
using (SQLiteCommand command = new SQLiteCommand("select last_insert_rowid()", connection))
{
return Convert.ToInt32(command.ExecuteScalar());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return -1;
}
finally
{
connection.Close();
}
}


public int InsertThematic(string name)
=> ExecuteNonQueryWithId("INSERT INTO Thematic (Name) VALUES (@Name)", new SQLiteParameter("@Name", name));
private int ExecuteNonQueryWithId(string query, params SQLiteParameter[] parameters)
{
try
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
}
using (SQLiteCommand command = new SQLiteCommand("select last_insert_rowid()", connection))
{
return Convert.ToInt32(command.ExecuteScalar());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return -1;
}
finally
{
connection.Close();
}
}


public int InsertThematic(string name)
=> ExecuteNonQueryWithId("INSERT INTO Thematic (Name) VALUES (@Name)", new SQLiteParameter("@Name", name));
Silme94
Silme943w ago
it will work the same for
string themeQuery = "SELECT * FROM Theme";
using (SQLiteCommand command = new SQLiteCommand(themeQuery, externalConnection))
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int oldThematicId = Convert.ToInt32(reader["ThematicId"]);
int newThematicId = thematicMapping[oldThematicId];
string name = reader["Name"].ToString();
InsertTheme(newThematicId, name);
int newId = GetLastInsertId();
themeMapping[Convert.ToInt32(reader["Id"])] = newId;
}
}

string questionQuery = "SELECT * FROM Question";
using (SQLiteCommand command = new SQLiteCommand(questionQuery, externalConnection))
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int oldThemeId = Convert.ToInt32(reader["ThemeId"]);
int newThemeId = themeMapping[oldThemeId];
string text = reader["Text"].ToString();
InsertQuestion(newThemeId, text);
int newId = GetLastInsertId();
questionMapping[Convert.ToInt32(reader["Id"])] = newId;
}
}
string themeQuery = "SELECT * FROM Theme";
using (SQLiteCommand command = new SQLiteCommand(themeQuery, externalConnection))
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int oldThematicId = Convert.ToInt32(reader["ThematicId"]);
int newThematicId = thematicMapping[oldThematicId];
string name = reader["Name"].ToString();
InsertTheme(newThematicId, name);
int newId = GetLastInsertId();
themeMapping[Convert.ToInt32(reader["Id"])] = newId;
}
}

string questionQuery = "SELECT * FROM Question";
using (SQLiteCommand command = new SQLiteCommand(questionQuery, externalConnection))
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int oldThemeId = Convert.ToInt32(reader["ThemeId"]);
int newThemeId = themeMapping[oldThemeId];
string text = reader["Text"].ToString();
InsertQuestion(newThemeId, text);
int newId = GetLastInsertId();
questionMapping[Convert.ToInt32(reader["Id"])] = newId;
}
}
?
viceroypenguin
yeah, you have to do the same for Theme and Question. because the principle of the connecitno closing is the same across all of them
Silme94
Silme943w ago
it still return me 0
viceroypenguin
works for me.
No description
Want results from more Discord servers?
Add your server
More Posts
Enter and move buttonsso I have a group box on top of which there is a panel and there are buttons created by man. I want Teaching C# - Basic and Advanced topics (that are must learn)I will be making a learning guide for my workers who will start soon. I am currently making a colleDatabase suffix builderQuestion, is doing something like this acceptable? ```cs public class EditorDbContext : DbContex✅ I need help with compiling an console application exe with the Roslyn compilerHi there, ive been working on a funky little program for fun, and its basically supposed to be a wei✅ I need help with positioning the news under weather, stock, and time. https://paste.ofcode.org/v3i tried by using Cursorposition but it didn't work for some reason.Put a pdf in a formI want to select a pdf from the pc and format it in a button or something. When I press the button t✅ EF Core - Not getting updated data from PostgreSQL tableI have the following method in my `DbService.cs` ```csharp public async Task<long?> GetLastDumpTimeSHow do I implement the Chrome API Sidepanel with a Blazor browser extension?Posted the question with more details here: https://stackoverflow.com/questions/78640477/how-do-i-u✅ Need Help making a Loader for a game.I just need someone to tell me what to Fix. 2 Erros and 1 warrning not really sure what to do and YeQuestPDF Why does the document become empty after adding the table and how to fix it?I have a pdf file that I generate using code, I'm trying to add a table to it that is contained in c