Search code examples
c#asp.netmysqlwebmethod

C# webmethod to load data from MySQL on page scroll


Im working on a web application in which I need to show data from MySQL on page scroll. In my codebehind I have a [WebMethod] GetData() which connects to MySQL_DB. However I could only able to show first column in my table, below is the code. I want to show a complete table or specific columns. How can I achieve this??

CodeBehind

[WebMethod]
public static string GetDataFromServer()
{
    DataSet ds = new DataSet();
    string connString = "conString";
    MySqlConnection mycon = new MySqlConnection(connString);
    MySqlCommand cmd = new MySqlCommand("select * from rest", mycon);
    MySqlDataAdapter adp = new MySqlDataAdapter(cmd);

    int retVal = adp.Fill(ds);

    string resp = string.Empty;
    for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
    {
        string strComment = string.Empty;
        if (ds.Tables != null)
        {
            if (ds.Tables[0] != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count >= i - 1)
                    {
                        if (ds.Tables[0].Rows[i - 1][0] != DBNull.Value)
                        {
                            //GridView1.DataSource();
                            strComment = ds.Tables[0].Rows[i - 1][0].ToString();
                        }
                    }
                }
            }
        }
        resp += "<p><span>"  + "</span> " + strComment + "</p>";
    }
    return resp;
}

ASPX

        <div id="wra" style="height:300px;overflow:auto">
            <asp:GridView ID="GridView1" runat="server" ></asp:GridView>
        </div>

Solution

  • class TableName
    {
    public _columnName
    {
    get;
    set;
    }
    ...
    }
    

    Include all columns of a table as above.Then fetch all required records from database. Convert database objects into bean class objects and add it in list

    List<TableName> listTableName=new List<TableName>();
    TableName objTableName=null;
    

    //make a loop that goes on all fetched records of database objTableName._columnName=yourdatabasecolumnvalue

    and objTableName to list inside loop and return list from service