Код: Выделить всё
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 = '';
Value.ChangedBy = '';
$.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);
}
});
}
Поэтому я решил параметризовать этот раздел, собрав все поля из таблицы базы данных и добавив их через такой цикл...
Код: Выделить всё
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);
}
});
}
А вот мой код GetData на 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.GetXml();
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... o-database
Мобильная версия