I am trying to save array of data in database how to achieve it. Here I have a form where three inputs PRODUCT_ID, TDC_NO, REVISION are there along with array list of values generated dynamically(sizeMin ,sizeMax ,tolMin ,tolMax ).I want to store those dynamically generated values in “Prop_detail” whose structure I have mentioned at below and PRODUCT_ID, TDC_NO, REVISION values in “tdcProduct1” table whose structure I designed below.how to pass those array list of values from server side to database and how to store further in database.
<script type="text/javascript">
$(document).on("click", "[id*=btnFrmSubmit]", function () {
var user = {};
user.PRODUCT_ID = 1;
user.TDC_NO = $("[id*=Tdc_No]").val();
user.REVISION = $("#Revision").text();
/* Creating Array object as WireDimDetails to add in user object*/
var WireDimDetails = new Array();
$("#WireDimTbl tbody tr").each(function () {
var row = $(this);
/* Declare and sets the WireDimDetail object with the property which will add in WireDimDetails array object*/
var WireDimDetail = {};
var sizeMin = row.find("[id^=SizeMin]").val();
/* Checking if control exist or not else assign empty value in sizeMax*/
var sizeMax = row.find("[id^=SizeMax]") != "undefined" ? row.find("[id^=SizeMax]").val() : "";
var tolMin = row.find("[id^=TolMin]").val();
var tolMax = row.find("[id^=TolMax]").val();
/*Sets the Values of controls */
WireDimDetail.SizeMin = sizeMin;
WireDimDetail.SizeMax = sizeMax;
WireDimDetail.TolMin = tolMin;
WireDimDetail.TolMax = tolMax;
/*Add WireDimDetail object in WireDimDetails Array object*/
WireDimDetails.push(WireDimDetail);
})
/*Add WireDimDetails array of object to user object*/
user.WireDimDetails = WireDimDetails;
$.ajax({
type: "POST",
url: "TDC.aspx/SaveFrmDetails",
data: JSON.stringify({ user: user, }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Data has been added successfully.");
window.location.reload();
},
error: function (response) { alert(response.responseText); }
});
</script>
Server side
[WebMethod]
public static void SaveFrmDetails(User user)
{
string connectionString = ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString;
using (OracleConnection con = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand("INSERT INTO TDC_PRODUCT1(PRODUCT_ID,TDC_NO, REVISION) VALUES (:PRODUCT_ID,:TDC_NO,:REVISION )", con))
{
cmd.CommandType = CommandType.Text;
List<WireDimDetail> wireDimDetails = user.WireDimDetails;
for (int i = 0; i < wireDimDetails.Count; i++)
{
WireDimDetail wireDimDetail = wireDimDetails[i];
string sizeMin = wireDimDetail.SizeMin;
string sizeMax = !string.IsNullOrEmpty(wireDimDetail.SizeMax) ? wireDimDetail.SizeMax : "0"; // set default value
string tolMin = wireDimDetail.TolMin;
string tolMax = wireDimDetail.TolMax;
}
cmd.Parameters.AddWithValue(":PRODUCT_ID", user.PRODUCT_ID);
cmd.Parameters.AddWithValue(":TDC_NO", user.TDC_NO);
cmd.Parameters.AddWithValue(":REVISION", user.REVISION);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Table structure For saving data and array list of data “tdcProduct1” table consist of three rows Productid|Tdc_no|Revision
second table “Prop_detail” Tdc_no|Tdc_property
My concern is how to store array of data in “Prop_detail” table at the same time while storing data in “tdcProduct1” using SaveFrmDetails.
Any idea would be appreciated.