This is my simple code for inserting data to database and it’s working fine. Table name is AccountMaster
function Insert() {
var Value = {};
Value.Id = $("[id*=hdnId]").val();
Value.AccountName = $("[id*=txtAccountName]").val();
Value.Catagory = $("[id*=txtCatagory]").val();
Value.CatagorySr = $("[id*=hdnCatagorySr]").val();
Value.HeadName = $("[id*=hdnHeadName]").val();
Value.HeadId = $("[id*=hdnHeadId]").val();
Value.BalanceSheetSr = $("[id*=hdnBalanceSheetSr]").val();
Value.AccountTypeName = $("[id*=txtAccountTypeName]").val();
Value.AccountTypeId = $("[id*=hdnAccountTypeId]").val();
Value.AccountTypeSr = $("[id*=hdnAccountTypeSr]").val();
Value.Sr = $("[id*=txtSr]").val();
Value.AccountCode = $("[id*=hdnAccountCode]").val();
Value.OpeningBalance = $("[id*=txtOpeningBalance]").val();
Value.Nature = $("[id*=ddlNature]").val();
Value.ClosingBalance = $("[id*=txtClosingBalance]").val();
Value.PrevYearIE = $("[id*=txtPrevYearIE]").val();
Value.TINNo = $("[id*=txtTINNo]").val();
Value.GSTNo = $("[id*=txtGSTNo]").val();
Value.Remark = $("[id*=txtRemark]").val();
Value.Address = $("[id*=txtAddress]").val();
Value.EntryBy = '<%=Session["Name"]%>';
Value.ChangedBy = '<%=Session["Name"]%>';
$.ajax({
type: "POST",
url: "AccountMaster.aspx/InsertUpdate",
data: '{Value: ' + JSON.stringify(Value) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Saved Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
Above code shows that every time I have to add fields to Value
one by one. If I have more than 100 fields in a database table I have to add it one by one which is very painful sometimes.
So I decided to parameterize that section by collecting all the fields from database table and add them through loop like this…
function Insert(FormName, DivName) {
$.ajax({
type: "POST",
url: "" + FormName + ".aspx/GetData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var Records = xml.find("Table");
if (Records.length > 0) {
var Main = {};
$.each(Records, function () {
var record = $(this);
var ColName = record.find("Column_name").text(); //Getting Column names from database
var ColValue = GetColumnsValue(ColName, DivName); // getting columns value in webform.
Main.ColName = ColValue; // adding columns value to Main, this line is not working
});
$.ajax({
type: "POST",
url: "" + FormName + ".aspx/Insert",
data: "{MainValues: " + JSON.stringify(Main) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
}).done(function () {
});
}
},
error: function (d) {
alert(d.responseText);
}
});
}
As usual above method thinks ColName
is the field of database table.
And here is my GetData
code in c#
[WebMethod]
public static string GetData()
{
using (SqlCommand Cmd = new SqlCommand())
{
using (SqlConnection Con = Connections.Create())
{
Cmd.Connection = Con;
Cmd.CommandText = "GetColumnList_pro".Trim();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("Option", "GetColumnList".Trim());
Cmd.Parameters.AddWithValue("Table", "DummyTable".Trim());
using (SqlDataAdapter Adp = new SqlDataAdapter())
{
using (DataSet Ds = new DataSet())
{
Con.Open();
Adp.SelectCommand = Cmd;
Adp.Fill(Ds);
return Ds.Geml();
}
}
}
}
}
3