Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 调用后面页面上的方法值_Asp.net - Fatal编程技术网

Asp.net 调用后面页面上的方法值

Asp.net 调用后面页面上的方法值,asp.net,Asp.net,我在mail.cs文件中有这个方法,它接受一个参数,调用存储过程并从数据库返回值 public void select(string type) { string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { us

我在mail.cs文件中有这个方法,它接受一个参数,调用存储过程并从数据库返回值

   public void select(string type)
{
    string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("EmailSetup_CRUD"))
        {
            cmd.Parameters.AddWithValue("@Action", "SELECT");
            cmd.Parameters.AddWithValue("@Type", type);






        }

    }
}
我想在page behind上调用此方法的返回值,并将其与label绑定。请告诉我怎么做

在我的页面后面的页面加载中,我已经这样做了

    mail callmail = new mail()
现在我了解了label1.text如何将返回值分配给label1.text


方法返回5列,我的页面上有5个标签,因此每列将分配给每个标签。

根据我在下面代码中进行的讨论和假设

mail.cs

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace tempApp4
{
    public class mail
    {
        public DataTable select(string type)
        {            
            string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("EmailSetup_CRUD", con))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "SELECT");
                    cmd.Parameters.AddWithValue("@Type", type);

                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataSet = new DataSet();

                    adapter.Fill(dataSet);

                    return dataSet.Tables[0];
                }

            }
        }
    }
}
WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="tempApp4.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="label1" runat="server" />
        <asp:Label ID="label2" runat="server" />
        <asp:Label ID="label3" runat="server" />
        <asp:Label ID="label4" runat="server" />
        <asp:Label ID="label5" runat="server" />
    </form>
</body>
</html>
SqlDataAdapter“adapter”用于从数据库中获取记录并将其填充到数据集中。当您的过程返回单个表时,数据集包含多个表。“select”方法在末尾返回单个表,如下所示:

return dataSet.Tables[0];
因此,“select”方法的返回类型为“DataTable”。然后,最后使用mail类的对象访问code behind中的返回值,并通过访问列填充标签,如下所示:

label1.Text = Convert.ToString(table.Rows[0][0]);
“table.Rows[0][0]中的第一个0表示行,第二个表示列。对于列(可选),您可以将列的名称指定为:

label1.Text = Convert.ToString(table.Rows[0]["COLUMN_NAME"]);
因为我不知道列名,所以我正在使用索引


希望对您有所帮助

您的代码不完整,您从过程中获取的数据类型以及获取记录的方式DataReader或DataAdapter请指定其返回字符串值,请建议DataReader或data Adapter使用哪种方式更好此“cmd.Parameters.AddWithValue”(@Type)之后您在做什么“,类型);“?不,在此之后我不会做任何事情。我不知道在此之后如何获取数据。您应该了解DataReader、DataAdapter或ADO.NET的一般情况,以了解其工作原理
label1.Text = Convert.ToString(table.Rows[0]["COLUMN_NAME"]);