Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello Dear
am create a web method in Asp.net C#
C#
public static string Getdata(string Id)
       {
           string SQL = "select * from testtable where UserId='" + Id + "'";
           DataTable DATA = new DataTable();
           SqlDataAdapter DTA = new SqlDataAdapter(SQL, cnnx);
           DTA.Fill(DATA);
           if (DATA.Rows.Count > 0)
           {






           }
           return "";

This is my code
now i want to return all rows value to get value in ajax success
JavaScript
\  <script>
        $(document).ready(function () {
            $("#Btnclick").click(function () {
                $.ajax({
                    type: "POST",
                    url: "WebForm1.aspx/Getdata",
                    data: "{'Id':'" + $("#textid").val() + "'}",
                    datatype: "JSON",
                    contentType: "application/json; charset=ut-8",
                    success:function(data)
                    {
                        alert(data.d);
                       
                    },
                    error: function () {
                        alert("error");
                    }
                   

                });

            });



        });
    </script>


What I have tried:

Get all rows value and return value in ajax
Posted
Updated 12-Apr-16 19:03pm
Comments
CHill60 12-Apr-16 17:14pm    
Your datatable DTA will contain all of the rows that match that Id (as well as all of the columns on the table). If you want to return all the rows in the table then drop the where from the query.
You should also not use concatenated strings for your query, especially if Id could contain user input. User parameterised queries instead
Arslan saif 12-Apr-16 17:16pm    
can you give me some code example
CHill60 12-Apr-16 18:25pm    
If you want to reply to a post use the "Reply" link next to it so that the poster is notified. I've posted a solution

To use Parameterized queries in your GetData function do something like this:
C#
public static DataTable GetSingleData(string Id)
{
    string connectionstring = Properties.Settings.Default.SandboxConnectionString;
    string SQL = "select * from testtable where UserId=@Id";
    DataTable DATA = new DataTable();
    using (var cnnx = new SqlConnection(connectionstring))
    {
        using (SqlDataAdapter DTA = new SqlDataAdapter(SQL, cnnx))
        {
            SqlParameter parm = DTA.SelectCommand.Parameters.AddWithValue("@Id", Id);
            DTA.Fill(DATA);
            if (DATA.Rows.Count > 0)
            {
                // whatever you want to do if you have rows
            }
        }
    }
    return DATA;    // Information you want will be in Row 0 (hopefully)
}

To get all the rows instead of just a single one then this is the sort of thing you need:
C#
public static DataTable GetAllData()
{
    string connectionstring = Properties.Settings.Default.SandboxConnectionString;
    string SQL = "select * from testtable"; // should really list the columns you want
    DataTable DATA = new DataTable();
    using (var cnnx = new SqlConnection(connectionstring))
    {
        using (SqlDataAdapter DTA = new SqlDataAdapter(SQL, cnnx))
        {
            DTA.Fill(DATA);
            if (DATA.Rows.Count > 0)
            {
                // whatever you want to do if you have rows
            }
        }
    }
    return DATA;    // Hopefully there will be several rows in the datatable
}

You can then get at the data in the DataTable(s) like this:
Single item
C#
var dtSingle = GetSingleData(17);
var UserName = dtSingle.Rows[0]["UserName"];

All items
C#
var dtAll = GetAllData();
foreach (DataRow dr in dtAll)
{
    var UserName = dr["UserName"];
}

OR
C#
for (int i = 0; i < dtAll.Rows.Count; i++)
{
    var UserName = dtAll[i]["UserName"];
}

(This assumes that there is a column on the database table testtable called "UserName" - replace this with whatever data you are trying to retrieve)
 
Share this answer
 
Comments
Arslan saif 13-Apr-16 5:50am    
How to get whole Rows data in for each and return data into ajax for showing in screen with generate HTML
CHill60 13-Apr-16 6:01am    
See Solution 2 for getting the data to client side
From DataBase to model :- you can use DataAdapter class.
From model to ClientSide :- you can use DataSet.ToXml() function.With the help of this method you can fill your grid in client side.
 
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