C#将两个Mysql列名合并为一个

时间:2015-11-11 13:34:23

标签: c# mysql asp.net

在C#Asp.net中我试图将两列数据合并为一个并显示在下拉列表中,但是当我无法以DD_TName1.DataTextField = "FirstName" + ' ' + "LastName";的方式执行此操作时,会听到问题并且consol错误是

  

Sys.WebForms.PageRequestManagerServerErrorException:Sys.WebForms.PageRequestManagerServerErrorException:DataBinding:' System.Data.Common.DataRecordInternal'不包含名称为' FirstName LastName'。

的属性

根据Console错误,将要转换为字符串的列名称作为FirstName LastName由db读取为一个列名称

using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand("SELECT * from euser_teacher WHERE SchoolId='" + Schoolid + "' and StaffType='Teaching' and IsDeleted='0'  order by FirstName     ASC"))
            {

                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                DD_TName1.DataSource = cmd.ExecuteReader();


                DD_TName1.DataTextField = "FirstName" + ' ' + "LastName";
                DD_TName1.DataValueField = "TeacherId";
                DD_TName1.DataBind();
                con.Close();
            }
        }

2 个答案:

答案 0 :(得分:3)

如何修改SELECT-Statement?

MySQL的:

SELECT TeacherId, Concat(FirstName, ' ',LastName) AS FirstLast from euser_teacher WHERE

C#

DD_TName1.DataTextField = "FirstLast";
DD_TName1.DataValueField = "TeacherId";

答案 1 :(得分:1)

尝试从select *更改sql以选择FirstName,LastName(所需的字段名称)