我想要SQL數據庫表格在GridView.Here中綁定,我使用Ajax以通過使用HTTP POST動詞的'url'調用名爲'GetProducts'的Web方法。它執行select查詢並返回數據。這裏是代碼,但是在'p.ToArray()'中顯示並顯示錯誤。它表示不能隱式轉換。請幫助我使此代碼能夠運行。在代碼的其他部分的任何錯誤: 這是.aspx.cs頁中的代碼:將列表複製到新陣列時出錯
namespace AvailableProductsWithAjax
{
public partial class AvailableProductsWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindDummyRow();
}
// GridView1.Columns[0].Visible = false;
}
private void BindDummyRow()
{
DataTable dummy = new DataTable();
//dummy.Columns.Add("Product_Id");
dummy.Columns.Add("Product_Name");
dummy.Columns.Add("Product_Description");
dummy.Columns.Add("Product_Category");
dummy.Columns.Add("Product_Price");
dummy.Columns.Add("Product_Quantity");
dummy.Rows.Add();
GridView1.DataSource = dummy;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
[WebMethod]
public static Products GetProducts()
{
List<Products> p = new List<Products>();
string query = "Select Product_Name, Product_Description, Product_Category, Product_Price, Product_Quantity from Items";
String cs = ConfigurationManager.ConnectionStrings["WebShopDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
SqlCommand cmd = new SqlCommand();
sda.SelectCommand = cmd;
cmd.Connection = con;
cmd.CommandText = query;
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow dtRow in dt.Rows)
{
Products ps = new Products();
ps.Products_Name = dtRow["Product_Name"].ToString();
ps.Products_Category = dtRow["Product_Category"].ToString();
ps.Products_Description = dtRow["Product_Description"].ToString();
ps.Products_Price = dtRow["Product_Price"].ToString();
ps.Products_Quantity = dtRow["Product_Quantity"].ToString();
}
}
}
return p.ToArray();
// Here is the error, cannot be converted implicitly
}
public class Products
public int Products_Id { get; set; }
public string Products_Name { get; set; }
public string Products_Description { get; set; }
public string Products_Category { get; set; }
public string Products_Price { get; set; }
public string Products_Quantity { get; set; }
}
}
}
這裏是JQuery的阿賈克斯功能:
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "AvailableProductsWebForm.aspx/GetProducts",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
success: OnSuccess,
failure: function (data) {
alert(data.d +" Its a failure");
},
error: function (data) {
alert(data.d +" It's an error");
}
});
});
function OnSuccess() {
$("#GridView1").empty();
if(data.d.length>0)
{
$("#GridView1").append("<tr><th>Product_Name</th> <th>Product_Category</th> <th>Product_Description </th> <th>Product_Price</th> <th> Product_Quantity</th></tr>");
for(var i=0; i<data.d.length; i++)
{
$("#GridView1").append("<tr><td>" + data.d[i].Product_Name +"</td> <td>"
+ data.d[i].Product_Category + "</td> <td>"
+ data.d[i].Product_Description +
data.d[i].Product_Price + "</td> <td>" +
data.d[i].Product_Quantity + "</td> <td>"
);
}
}
}
</script>
這裏是我的數據庫查詢:
USE [WebShop]
GO
CREATE TABLE [dbo].[Items](
[Product_Id] [int] IDENTITY(1,1) NOT NULL,
[Product_Name] [varchar](50) NULL,
[Product_Description] [varchar](50) NULL,
[Product_Category] [varchar](50) NULL,
[Product_Price] [varchar](50) NULL,
[Product_Quantity] [varchar](50) NULL,
GO
請幫我運行這段代碼。