Reputation: 339
I have everything else setup but now i simply need to add an entry to my table in access.It would be simple to just show my data in a data grid view and use a binding source with a binding navigator, but i here is the scenario : I have to tables, One with Song Name , Artist and Genre and another table with only genre, the two tables have a relationship(with referential integrity) with relation to genre. I would like to call a inputbox in a C# program to simply add a new genre to the genre table. Now since there is no data grid view or any other control to easily add a entry, what is the simple procedure to add a entry into your table?
Here is my code thus far for the appropriate event handler :
private void newGenreToolStripMenuItem_Click(object sender, EventArgs e)
{
//(cnSongs)Connection delacred and instantiated elsewhere
cnSongs.Open();
string input = Interaction.InputBox("Genre ", "New Genre", "Type genre here", -1, -1);
string InsertSql = "INSERT INTO tblGenres (Genre) VALUES (" + input + ")";
OleDbCommand cmdInsert = new OleDbCommand(InsertSql, cnSongs);
//(daSongs)Data Adapter delacred and instantiated elsewhere
daSongs = new OleDbDataAdapter(InsertSql, cnSongs);
daSongs.InsertCommand = cmdInsert;
cmdInsert.ExecuteNonQuery();
cnSongs.Close();
}
I did research and only got the sql statement needed, which was useful but i need to know how to exacute it in code.
Thank you for your time.
Upvotes: 1
Views: 1930
Reputation: 10976
This is an example of how to use OleDbParameter
. However, I can't see why your code wouldn't produce new genres.
private void newGenreToolStripMenuItem_Click(object sender, EventArgs e)
{
//(cnSongs)Connection delacred and instantiated elsewhere
cnSongs.Open();
try {
string input = Interaction.InputBox("Genre ", "New Genre", "Type genre here", -1, -1);
Console.WriteLine("User input " + input);
string InsertSql = "Insert Into tblGenres (Genre) Values (?)";
OleDbCommand cmdInsert = new OleDbCommand(InsertSql, cnSongs);
cmdInsert.Parameters.Add(new OleDbParameter("genre", input));
int i = cmdInsert.ExecuteNonQuery();
Console.WriteLine("Inseted " + i.toString() + " row(s)");
} finally {
cnSongs.Close();
}
}
Upvotes: 1