我希望SQL数据库表在GridView.Bere中绑定,我使用Ajax来调用名为' GetProducts'的Web方法。通过' url',使用HTTP POST动词。它执行选择查询并返回数据。这是代码,但是显示和错误在'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 Ajax函数:
<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
请帮我运行此代码。
答案 0 :(得分:0)
您的返回类型为Products
,但您尝试返回List<Products>
将方法签名更改为
public static List<Products> GetProducts()
其次,您没有填写返回值。变量p仍为空
所以你应该在foreach循环中将ps添加到p
p.Add(ps);
答案 1 :(得分:0)
您应该在Products[]
而不是Products
所以你的代码应该是
[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";
如果需要更多帮助,请告诉我。