I checked the code from link Trying to save array of data in database using c#.
And mofofied the code as per the given table structure.
Refer the below code and implement it as per your code logic.
C#
[WebMethod]
public static void SaveFrmDetails(User user)
{
string tdcProduct1Query = "INSERT INTO TDC_PRODUCT1(PRODUCT_ID,TDC_NO, REVISION) VALUES (@PRODUCT_ID,@TDC_NO,@REVISION)";
SqlParameter[] tdcProduct1Parameters = new SqlParameter[]
{
new SqlParameter("@PRODUCT_ID", user.PRODUCT_ID),
new SqlParameter("@TDC_NO", user.TDC_NO),
new SqlParameter("@REVISION", user.REVISION)
};
// Insert record into tdcProduct1
InsertRecords(tdcProduct1Query, tdcProduct1Parameters);
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;
InsertPropertyDetails(user, PropertyText.SizeMin, i + 1, wireDimDetail.SizeMin);
if (!string.IsNullOrEmpty(wireDimDetail.SizeMax))
{
InsertPropertyDetails(user, PropertyText.SizeMax, i + 1, wireDimDetail.SizeMax);
}
InsertPropertyDetails(user, PropertyText.TolMin, i + 1, wireDimDetail.TolMin);
InsertPropertyDetails(user, PropertyText.TolMax, i + 1, wireDimDetail.TolMax);
}
}
private static void InsertPropertyDetails(User user, string propertyName, int rowNo, string tdcProperty)
{
string propDetailQuery = "INSERT INTO Prop_detail(TDC_NO,RowNo,Prop_Name,Tdc_Property) VALUES (@TDC_NO,@RowNo,@Prop_Name,@Tdc_Property)";
SqlParameter[] propDetailParameters = new SqlParameter[]
{
new SqlParameter("@TDC_NO", user.TDC_NO),
new SqlParameter("@RowNo", rowNo),
new SqlParameter("@Prop_Name", propertyName),
new SqlParameter("@Tdc_Property", tdcProperty)
};
InsertRecords(propDetailQuery, propDetailParameters);
}
public static void InsertRecords(string query, SqlParameter[] sqlParameters = null)
{
string connectionString = ConfigurationManager.ConnectionStrings["dbpik"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
if (sqlParameters.Length > 0)
{
cmd.Parameters.AddRange(sqlParameters);
}
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public class User
{
public string PRODUCT_ID { get; set; }
public string TDC_NO { get; set; }
public string REVISION { get; set; }
public List<WireDimDetail> WireDimDetails { get; set; }
}
public class WireDimDetail
{
public string SizeMin { get; set; }
public string SizeMax { get; set; }
public string TolMin { get; set; }
public string TolMax { get; set; }
}
public class PropertyText
{
public const string SizeMin = "SizeMin";
public const string SizeMax = "SizeMax";
public const string TolMin = "TolMin";
public const string TolMax = "TolMax";
}