根据下拉列表插入数据库

时间:2017-07-13 08:53:30

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

我正在构建一个简单的表单来添加,并从数据库表中删除用户。删除用户部分有一个下拉列表控件,可以在数据库中的每次更改后刷新。我设法完成此更新的唯一方法是在每次更改后刷新页面。我现在有一个非常简单的问题:我想通过一个简单的标签在每个动作之后显示一条消息。但是现在我已经添加了页面刷新,它不再显示,即使使用线程睡眠语句也是如此。我怎么能这样做?

请参阅下面的C#代码

ViewPager.PageTransformer

1 个答案:

答案 0 :(得分:0)

您不应重定向到刷新下拉列表,请尝试以下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            BindRemUserList();
        }
    }

    protected void BindRemUserList()
    {
        string CS = ConfigurationManager.ConnectionStrings["EP_PLANNING_NEW"].ConnectionString;
        using(SqlConnection con = new SqlConnection(CS))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT [ID],[First Name] + ' ' + [Last Name] AS [Full Name] FROM [dbo].[team_members]"))
            {

                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;

                try
                {
                    con.Open();
                    RemUserList.DataSource = cmd.ExecuteReader();
                    RemUserList.DataTextField = "Full Name";
                    RemUserList.DataValueField = "ID";
                    RemUserList.DataBind();
                }

                catch (Exception ex)
                {
                    throw ex;
                }

                finally
                {
                    con.Close();
                    con.Dispose();
                }

            }
        }

        RemUserList.Items.Insert(0, new ListItem("- Select Team Member to remove -", "0"));
    }

    protected void RemUserList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void AddUserButton_Click(object sender, EventArgs e)
    {
        string CS = ConfigurationManager.ConnectionStrings["EP_PLANNING_NEW"].ConnectionString;
        using(SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[team_members] ([First Name],[Last Name]) VALUES (@FirstName,@LastName)", con);
            cmd.Parameters.AddWithValue("@FirstName", AddUserFirstName.Text);
            cmd.Parameters.AddWithValue("@LastName", AddUserLastName.Text);

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }

            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                con.Close();
                con.Dispose();
            }
            MsgLbl.Text = "** User Successfully Added **";
            AddUserFirstName.Text = "";
            AddUserLastName.Text = "";
            BindRemUserList();
        }
    }

    protected void RemUserButton_Click(object sender, EventArgs e)
    {
        string CS = ConfigurationManager.ConnectionStrings["EP_PLANNING_NEW"].ConnectionString;
        using(SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM [dbo].[team_members] WHERE [ID] = @ID", con);
            cmd.Parameters.AddWithValue("@ID", RemUserList.SelectedValue);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        MsgLbl.Text = "** User Successfully Removed **";
        BindRemUserList();
    }
}