private void dataGrid_UnloadingRow(object sender, DataGridRowEventArgs e) { if (((DataGrid)sender).SelectedItem != null || ((DataGrid)sender).CurrentItem == null) { return; } JobApplicationJobBoards myJobApplicationJobBoards = (JobApplicationJobBoards)((DataGrid)sender).CurrentItem; jobBoardManager.Delete(myJobApplicationJobBoards.Id); }
public void Upsert(JobApplicationJobBoards myJobBoard) { if (!String.IsNullOrEmpty(ConnectionString1.SqlConnString)) { SqlConnection thisConnection = new SqlConnection(ConnectionString1.SqlConnString); //Create Command object SqlCommand nonqueryCommand = thisConnection.CreateCommand(); try { // Open Connection thisConnection.Open(); Console.WriteLine("Connection Opened"); // Create INSERT statement with named parameters nonqueryCommand.CommandText = " IF NOT EXISTS(SELECT* FROM dbo.JobApplicationJobBoards WHERE ID = @Id)" + "INSERT INTO [dbo].[JobApplicationJobBoards] " + //" ([Id] " + " ([Name] " + " ,[Enabled]) " + " VALUES " + //" (@Id " + " (@Name " + " ,@Enabled) " + " ELSE " + "UPDATE [dbo].[JobApplicationJobBoards] " + " SET [Name] = @Name " + " ,[Enabled] = @Enabled " + " WHERE Id = @Id " + " "; // Add Parameters to Command Parameters collection nonqueryCommand.Parameters.Add("@Id", SqlDbType.Int); nonqueryCommand.Parameters.Add("@Name", SqlDbType.VarChar, 500); nonqueryCommand.Parameters.Add("@Enabled", SqlDbType.Bit); // Prepare command for repeated execution nonqueryCommand.Prepare(); // Data to be inserted nonqueryCommand.Parameters["@Id"].Value = myJobBoard.Id; nonqueryCommand.Parameters["@Name"].Value = myJobBoard.Name; nonqueryCommand.Parameters["@Enabled"].Value = myJobBoard.Enabled; Console.WriteLine("Executing {0}", nonqueryCommand.CommandText); Console.WriteLine("Number of rows affected : {0}", nonqueryCommand.ExecuteNonQuery()); } catch (SqlException ex) { // Display error Console.WriteLine("Error: " + ex.ToString()); } finally { // Close Connection thisConnection.Close(); Console.WriteLine("Connection Closed"); } } }
public ObservableCollection <JobApplicationJobBoards> GetJobBoards() { ObsCollJobApplicationJobBoards = new ObservableCollection <JobApplicationJobBoards>(); // ******************************************************************** // Code Generated by Ideal Tools Organizer at http://idealautomate.com // ******************************************************************** // Define Query String string queryString = "Select * from JobApplicationJobBoards " + ""; // Define .net fields to hold each column selected in query ObservableCollection <JobApplicationJobBoards> obsCollJobApplicationJobBoards = new ObservableCollection <JobApplicationJobBoards>(); Int32 int_JobApplicationJobBoards_Id; String str_JobApplicationJobBoards_Name; Boolean bool_JobApplicationJobBoards_Enabled; // Define a datatable that we will define columns in to match the columns // selected in the query. We will use sqldatareader to read the results // from the sql query one row at a time. Then we will add each of those // rows to the datatable - this is where you can modify the information // returned from the sql query one row at a time. Finally, we will // bind the table to the gridview. DataTable dt = new DataTable(); if (!String.IsNullOrEmpty(ConnectionString1.SqlConnString)) { using (SqlConnection connection = new SqlConnection(ConnectionString1.SqlConnString)) { SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); // Define a column in the table for each column that was selected in the sql query // We do this before the sqldatareader loop because the columns only need to be // defined once. DataColumn column = null; column = new DataColumn("JobApplicationJobBoards_Id", Type.GetType("System.Int32")); dt.Columns.Add(column); column = new DataColumn("JobApplicationJobBoards_Name", Type.GetType("System.String")); dt.Columns.Add(column); column = new DataColumn("JobApplicationJobBoards_Enabled", Type.GetType("System.Boolean")); dt.Columns.Add(column); // Read the results from the sql query one row at a time while (reader.Read()) { // define a new datatable row to hold the row read from the sql query DataRow dataRow = dt.NewRow(); // Move each field from the reader to a holding field in .net // ******************************************************************** // The holding field in .net is where you can alter the contents of the // field // ******************************************************************** // Then, you move the contents of the holding .net field to the column in // the datarow that you defined above JobApplicationJobBoards _JobApplicationJobBoards = new JobApplicationJobBoards(); if (!(reader.IsDBNull(0))) { int_JobApplicationJobBoards_Id = reader.GetInt32(0); dataRow["JobApplicationJobBoards_Id"] = int_JobApplicationJobBoards_Id; _JobApplicationJobBoards.Id = int_JobApplicationJobBoards_Id; } if (!(reader.IsDBNull(1))) { str_JobApplicationJobBoards_Name = reader.GetString(1); dataRow["JobApplicationJobBoards_Name"] = str_JobApplicationJobBoards_Name; _JobApplicationJobBoards.Name = str_JobApplicationJobBoards_Name; } if (!(reader.IsDBNull(2))) { bool_JobApplicationJobBoards_Enabled = reader.GetBoolean(2); dataRow["JobApplicationJobBoards_Enabled"] = bool_JobApplicationJobBoards_Enabled; _JobApplicationJobBoards.Enabled = bool_JobApplicationJobBoards_Enabled; } // Add the row to the datatable dt.Rows.Add(dataRow); ObsCollJobApplicationJobBoards.Add(_JobApplicationJobBoards); } // Call Close when done reading. reader.Close(); } } // assign the datatable as the datasource for the gridview and bind the gridview return(ObsCollJobApplicationJobBoards); }