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
C# 基于dropdownlist在数据库中插入_C#_Asp.net_Sql Server_Webforms - Fatal编程技术网

C# 基于dropdownlist在数据库中插入

C# 基于dropdownlist在数据库中插入,c#,asp.net,sql-server,webforms,C#,Asp.net,Sql Server,Webforms,我正在构建一个简单的表单,用于在数据库表中添加和删除用户。remove user部件有一个dropdownlist控件,该控件可以在数据库中的每次更改后刷新。我实现刷新的唯一方法是在每次更改后刷新页面。我现在有一个非常简单的问题:我希望在每个操作之后通过一个简单的标签显示一条消息。但是现在我添加了页面刷新,它不再显示,即使使用了线程睡眠语句。我怎么能这么做 请参见下面我的C代码 public partial class _Default : System.Web.UI.Page { protec

我正在构建一个简单的表单,用于在数据库表中添加和删除用户。remove user部件有一个dropdownlist控件,该控件可以在数据库中的每次更改后刷新。我实现刷新的唯一方法是在每次更改后刷新页面。我现在有一个非常简单的问题:我希望在每个操作之后通过一个简单的标签显示一条消息。但是现在我添加了页面刷新,它不再显示,即使使用了线程睡眠语句。我怎么能这么做

请参见下面我的C代码

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

        string CS = ConfigurationManager.ConnectionStrings["EP_PLANNING_NEW"].ConnectionString;
        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;
    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();
        }

        if (IsPostBack)
        {
            MsgLbl.Text = "** User Successfully Added **";
            //System.Threading.Thread.Sleep(2000);
            Response.Redirect(Request.RawUrl);
            AddUserFirstName.Text = "";
            AddUserLastName.Text = "";
        }
    }
}

protected void RemUserButton_Click(object sender, EventArgs e)
{
    string CS = ConfigurationManager.ConnectionStrings["EP_PLANNING_NEW"].ConnectionString;
    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();
        }

    }

    if (IsPostBack)
    {
        MsgLbl.Text = "** User Successfully Removed **";
        //System.Threading.Thread.Sleep(2000);
        Response.Redirect(Request.RawUrl);
    }
}

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

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();
    }
}

问题尽量简短:我们是否需要同时看到“添加”和“快速眼动”按钮?或者所有的
都使用
s?Thread.Sleep()很愚蠢,一定要让自己了解web编程的客户机/服务器特性。只是猜测一下,但我认为你的问题在于重定向()。这与同一通话中的标签、文本等设置相矛盾。下次将尝试遵循您的“善意”建议:-)祝您愉快!工作得很有魅力!!非常感谢:-)