Question
C# how do i insert a value into the databse using the textbox i update table.xaml.. I will supply all of the code that im
C# how do i insert a value into the databse using the textbox i update table.xaml.. I will supply all of the code that im using and have already written please help below will be the current classes and code that i have in my project..
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.OleDb; using System.IO; using System.Reflection;
namespace MyFinalGroupProject { public class clsDataAccess { ///
///
///
using (OleDbConnection conn = new OleDbConnection(sConnectionString)) { using (OleDbDataAdapter adapter = new OleDbDataAdapter()) {
//Open the connection to the database conn.Open();
//Add the information for the SelectCommand using the SQL statement and the connection object adapter.SelectCommand = new OleDbCommand(sSQL, conn); adapter.SelectCommand.CommandTimeout = 0;
//Fill up the DataSet with data adapter.Fill(ds); } }
//Set the number of values returned iRetVal = ds.Tables[0].Rows.Count;
//return the DataSet return ds; } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
///
using (OleDbConnection conn = new OleDbConnection(sConnectionString)) { using (OleDbDataAdapter adapter = new OleDbDataAdapter()) {
//Open the connection to the database conn.Open();
//Add the information for the SelectCommand using the SQL statement and the connection object adapter.SelectCommand = new OleDbCommand(sSQL, conn); adapter.SelectCommand.CommandTimeout = 0;
//Execute the scalar SQL statement obj = adapter.SelectCommand.ExecuteScalar(); } }
//See if the object is null if (obj == null) { //Return a blank return ""; } else { //Return the value return obj.ToString(); } } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
///
using (OleDbConnection conn = new OleDbConnection(sConnectionString)) { //Open the connection to the database conn.Open();
//Add the information for the SelectCommand using the SQL statement and the connection object OleDbCommand cmd = new OleDbCommand(sSQL, conn); cmd.CommandTimeout = 0;
//Execute the non query SQL statement iNumRows = cmd.ExecuteNonQuery(); }
//return the number of rows affected return iNumRows; } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } } } }
.......................................................................
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Data;
namespace MyFinalGroupProject { public class clsitem_Manager////////////////////////////////////////////this class managers flights from the database { //clsDataAccess db;
clsDataAccess db = new clsDataAccess(); DataSet ds = new DataSet(); BindingList
public BindingList
////////////////Steps for you to complete string sSQL = clsSQL.GetAllItems_SQL();/////////////////////////Get SQL to get all flights
int iRet = 0;
//Execute the sSQL statement using the clsDataAccess class to get the flights into a dataset ds = db.ExecuteSQLStatement(sSQL, ref iRet);
foreach (DataRow dr in ds.Tables[0].Rows) { lstItems.Add(new clsItem { ItemCode = dr[0].ToString(), ItemDesc = dr[1].ToString(), Cost = dr[2].ToString() }); numberOfRecords++; } return lstItems;
}
//public void insert1() //{ // foreach (DataRow row in ds.Rows) // { // if (row["IsActive"].ToString() == "Y") // { // numberOfRecords++; // } // }
//}
//public BindingList
// //lstFlights = ////////This is a list of flights that represents all flights from the database
// ////////////////Steps for you to complete // string sSQL = clsSQL.Insert();/////////////////////////Get SQL to get all flights
// int iRet = 0;
// //Execute the sSQL statement using the clsDataAccess class to get the flights into a dataset // ds = db.ExecuteSQLStatement(sSQL, ref iRet);
// foreach (DataRow dr in ds.Tables[0].Rows) // { // lstItems.Add(new clsItem { ItemCode = dr[0].ToString(), ItemDesc = dr[1].ToString(), Cost = dr[2].ToString() });
// } // return lstItems;
//}
}
public class clsItem { public string ItemCode { get; set; } public string ItemDesc { get; set; } public string Cost { get; set; }
}
}
................................
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace MyFinalGroupProject {
public class clsSQL { public static string GetAllItems_SQL() { string sSQL;
sSQL = "Select ItemCode, ItemDesc, Cost FROM ItemDesc";
return sSQL; }
public static string Insert() { string sSQL;
sSQL = "INSERT INTO ItemDesc(ItemCode, ItemDesc, Cost)" + "VALUES( 0 , 1 , 2)";
return sSQL; }
} }
----------------------------------------------------------------------
AutoGenerateColumns="False" CanUserResizeRows="True" CanUserResizeColumns="True" CanUserReorderColumns="True">
-----------------------
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.ComponentModel; using System.Data; using System.Drawing; using System.Reflection; using System.Data.OleDb; using System.IO; using System.Configuration;
namespace MyFinalGroupProject { ///
///
//BindingList
InitializeComponent(); item = new clsitem_Manager(); dataGrid1.ItemsSource = item.GetAllItemsFromDB(); }
private void ButtonInsert_Click(object sender, RoutedEventArgs e) { TextBoxItem.Text = item.numberOfRecords.ToString(); // item.insert1(); //dataGrid1.ItemsSource = item.GetAllItemsFromDB();
}
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started