Click here to Skip to main content
15,891,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i want to that,
i set content of the datatable multi columns values into dropdownlist.

I write that code,but not properly work on that.
Not hit the client side function i.e:-funChangeTable(ddlobj).
please some body help me for the above problem fixed.

What I have tried:

1:- My aspx code file is below:
--------------
ASP.NET
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ExcelGridDemo.WebForm1" %>

    <title>

<%--    
    --%>
        $(document).ready(function () {
            function funChangeTable(ddlObj) {
                var name = ddlObj.value;
            debugger;

                $.ajax({
                    url: 'WebForm1.aspx/GetTableData',
                    data: JSON.stringify({ tableName: name }),
                    type: 'post',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'JSON',
                    success: function (response) {
                        var json = JSON.parse(response.data);
                        generateTable(json);
                    },
                    error: function (a, b, c) {
                        console.log(a, b, c);
                    }
                });
            }

            function generateTable(json) {
                debugger;
                var $table = $('#tblDynamic');
                $table.find('thead').empty()
                $table.find('tbody').empty()
                if (json && json.length > 0) {
                    var header = json[0];
                    var columns = [];
                    for (var col in header) {
                        columns.push('<th>' + col + '</th>');
                    }
                    $table.find('thead').append('<tr>' + columns.join('') + '</tr>');
                    var rows = [];
                    for (var i = 0; i < json.length; i++) {
                        var row = json[i];
                        var tds = [];
                        for (var col in row) {
                            tds.push('<td>' + col + '</td>');
                        }
                        rows.push('<tr>' + tds.join() + '</tr>');
                    }
                    $table.find('tbody').append(rows.join(''));
                }

            }
        });
    
        <asp:dropdownlist runat="server" id="ddlTableNames" selectedindexchanged="funChangeTable(this)">
        
            <table id="tblDynamic" border="1"><thead></thead>            <tbody></tbody>        </table>




2:- My code behind file is below:
-----------------------

C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using Newtonsoft.Json; // Add this reference 
using System.Linq;
using System.Collections.Generic;

namespace ExcelGridDemo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //const string connString = "Data Source=PRGSERVER\\SQLEXPRESS;Initial Catalog=FinalNextErp;Integrated Security=True";
        const string connString = "Data Source=PRGSERVER\\SQLEXPRESS;Initial Catalog=FinalNextErp;Persist Security Info=True;User ID=sa;Password=dutta@1234;Pooling=True;Max Pool Size=200;Connect Timeout=0";

        protected void Page_Load(object sender, EventArgs e)
        {
            var data = GetAllTableNames();
            ddlTableNames.DataSource = data;    
            ddlTableNames.DataBind();
            ddlTableNames.Items.Insert(0, "Select a Table");
        }

        private static string[] GetAllTableNames()
        {
            SqlConnection con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand("select name from sys.tables", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt.AsEnumerable().Select(k => k[0] + "").ToArray();
        }

        [WebMethod]
        public static string GetTableData(string tableName)
        {
            if (GetAllTableNames().Contains(tableName)) // To avoid sql injection
            {
                SqlConnection con = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand("select * from " + tableName, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                string json = JsonConvert.SerializeObject(dt);
                return json;
            }
            else
                return "Table Name Not Found";
        }
    }
}
Posted
Updated 18-Aug-19 23:59pm
v2

1 solution

What is ddlObj ??

Why you are passing it in function funChangeTable ?

Why function funChangeTable is inside document.ready function ?

Your code should be
JavaScript
$(document).ready(function () {
funChangeTable();
});

function funChangeTable(){
var name = ddlObj.value;
          

                $.ajax({
                    url: 'WebForm1.aspx/GetTableData',
                    data: JSON.stringify({ tableName: name }),
                    type: 'post',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'JSON',
                    success: function (response) {
                        var json = JSON.parse(response.data);
                        generateTable(json);
                    },
                    error: function (a, b, c) {
                        console.log(a, b, c);
                    }
                });
            }

            function generateTable(json) {
                debugger;
                var $table = $('#tblDynamic');
                $table.find('thead').empty()
                $table.find('tbody').empty()
                if (json && json.length > 0) {
                    var header = json[0];
                    var columns = [];
                    for (var col in header) {
                        columns.push('<th>' + col + '</th>');
                    }
                    $table.find('thead').append('<tr>' + columns.join('') + '</tr>');
                    var rows = [];
                    for (var i = 0; i < json.length; i++) {

                        var row = json[i];

                        var tds = [];

                        for (var col in row) {

                            tds.push('<td>' + col + '</td>');
                        }
                        rows.push('<tr>' + tds.join() + '</tr>');
                    }
                    $table.find('tbody').append(rows.join(''));
                }

            }
        });


Let me know if it is not clear to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900