Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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
C# 使用存储过程和ASP.NET在GridView中显示名称而不是Id_C#_Asp.net_Sql Server - Fatal编程技术网

C# 使用存储过程和ASP.NET在GridView中显示名称而不是Id

C# 使用存储过程和ASP.NET在GridView中显示名称而不是Id,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我在ASP.NET中创建了一个GridView,它被数据绑定到存储过程 在我的存储过程中有另一个表中的customerID。如何显示客户的姓名而不是idnumber?谁能帮我一下吗 这是我的密码 private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using

我在ASP.NET中创建了一个GridView,它被数据绑定到存储过程

在我的存储过程中有另一个表中的
customerID
。如何显示客户的姓名而不是
id
number?谁能帮我一下吗

这是我的密码

private void BindGrid()
{        
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("usp_Counry"))
        {               
            cmd.Parameters.AddWithValue("@Action", "SELECT");             

            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                sda.SelectCommand = cmd;

                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);

                    GridView1.DataSource = dt;
                    GridView1.DataBind();                        
                }
            }
        }
    }
}

如果两个表都有CustomerId列(列名可能不同,值必须匹配),则可以在存储过程中使用join或subquery来获取客户的名称。 一个例子

SELECT c.CustomerId, c.CustomerName, o.OrderId 
FROM Orders AS o 
INNER JOIN Customers AS c ON c.CustomerId = o.CustomerId

您可以更改表名和列名,如果您需要查看包含空值的所有记录,也可以尝试外部联接

您必须向我们显示SP的一些示例输出数据以及GridView的标记。PS强烈建议不要使用。甚至可能是SP的内容。这应该在存储过程中完成-它不仅应该返回
客户ID
,还应该返回客户的姓名和您可能需要显示的任何其他信息谢谢,我会尝试。。所以只有我需要更改SP而不是C代码是吗?如果您没有自动填充列,那么您必须在GridView列中使用CustomerName(或您的列名)。@Dev视情况而定,您必须向我们展示任何人才能给出结论性答案。这不起作用,您需要提供更多信息。您的sp、关于表的信息以及关于GridView的信息。以及错误消息(如果有)