从存储的字符串中找到user_id,该字符串是字符串格式,id以分号存储

时间:2017-10-30 12:49:49

标签: c# sql asp.net .net sql-server

这里我可以用分号存储许多用户ID,我想要一个与下拉列表匹配的id选择user_id

<asp:GridView ID="TrainingTakenGridView" runat="server" AllowSorting="true" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" AutoGenerateSelectButton="false" CssClass="mGrid1" Width="100%" OnPageIndexChanging="TrainingTakenGridView_PageIndexChanging" DataKeyNames="training_id" OnSelectedIndexChanged="TrainingTakenGridView_SelectedIndexChanged" OnRowDataBound="TrainingTakenGridView_RowDataBound" >
                       <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="10" />
                        <AlternatingRowStyle CssClass="alt" />
                        <Columns>
                            <asp:BoundField HeaderText="Training Date" DataField="training_date" />
                            <asp:BoundField HeaderText="Topic" DataField="topic" />

                        </Columns>
                   </asp:GridView>

这是我的c#代码

  private DataTable bindTrainingAttendeedGridView(string condition)
    {

        DataTable dtGrid = new DataTable();
        con.ConnectionString = constr;
        //string query = "select training_id,convert(varchar(10),training_date,101) AS training_date,trainee,topic from tbl_training_attendee inner join tbl_userinfo on tbl_training_attendee.trainer=tbl_userinfo.user_id where tbl_training_attendee.training_id=" + condition;
        string query = "select training_id,trainee,topic from tbl_training_attendee where tbl_training_attendee.training_id=trainee "+condition;
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter sqladapter = new SqlDataAdapter(cmd);
        sqladapter.Fill(dtGrid);
        if (dtGrid.Rows.Count <= 0)
        {
            Label1.Text = "No Matching Training taken Entries Found, Thank You.";
        }
        else
        {
            Label1.Text = "";
        }
        //con.Open();
        return dtGrid;
    }
    protected void TrainingAttendenceGridViewBind(string condition)
    {
        try
        {
            TrainingAttendeedGridView.DataSource = bindTrainingAttendeedGridView(condition);
            TrainingAttendeedGridView.DataBind();

        }
        catch (Exception er)
        {
            throw er;
        }
    }
    protected void training_attendence_condition()
    {
        string condition = "";
        if (UserDropDownList.SelectedIndex > 0)
        {
            condition += "AND tbl_training_attendee.trainee='" + UserDropDownList.SelectedValue + "'";

        }

        TrainingAttendenceGridViewBind(condition);
    }

但是如果在数据库中记录可用,那么它仍然可以显示空字段。请帮帮我。

这是我的数据库,其中显示了受训者

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用此代码更改SQL查询:

select training_id,CONVERT(varchar(10),training_date,101) AS Training_date,topic,trainee from tbl_training_attendee where CONCAT( ',',trainee,',') like CONCAT( '%,','"+UserDropDownList.SelectedValue+"',',%' )