Hi KatieNgoc,
According to your requirement you need to create a table where you have to store user login detail and delete DropDownList data. And then get data from table according to user login, after that you have to check whether inserted data and your DropDownList data are same if same then have to delete.
Check below code.
HTML
Login.aspx
Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
<asp:Button Text="Loging" runat="server" OnClick="Loginn" />
CS.aspx
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
Login.aspx.cs
protected void Loginn(object sender, EventArgs e)
{
Session["UserName"] = txtName.Text;
Response.Redirect("CS.aspx");
}
Login.aspc.vb
Protected Sub Loginn(ByVal sender As Object, ByVal e As EventArgs)
Session("UserName") = txtName.Text
Response.Redirect("CS.aspx")
End Sub
CS.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["ListItems"] == null)
{
DataTable dt = DropDownItems();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT * FROM UserDetail WHERE UserDetail = @User", con);
cmd.Parameters.AddWithValue("@User", Session["UserName"].ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt1 = new DataTable();
da.Fill(dt1);
if (dt1.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt1.Rows.Count; j++)
{
if (dt.Rows[i]["Text"].ToString() == dt1.Rows[j]["SelectedText"].ToString())
{
dt.Rows[i].Delete();
}
}
}
}
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
Session["ListItems"] = dt;
}
}
}
private DataTable DropDownItems()
{
DataTable dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("Value");
dt.Rows.Add("Item1", "1");
dt.Rows.Add("Item2", "2");
dt.Rows.Add("Item3", "3");
dt.Rows.Add("Item4", "4");
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = Session["ListItems"] as DataTable;
ListItemCollection liCol = DropDownList1.Items;
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem li = liCol[i];
if (li.Selected)
{
dt.Rows[i].Delete();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("INSERT INTO UserDetail VALUES(@SelectedText,@SelectedVal,@UserName)", con);
cmd.Parameters.AddWithValue("@SelectedText", li.Text);
cmd.Parameters.AddWithValue("@SelectedVal", li.Value);
cmd.Parameters.AddWithValue("@UserName", Session["UserName"].ToString());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Session["ListItem"] = dt;
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Session("ListItems") Is Nothing Then
Dim dt As DataTable = DropDownItems()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM UserDetail WHERE UserDetail = @User", con)
cmd.Parameters.AddWithValue("@User", Session("UserName").ToString())
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt1 As DataTable = New DataTable()
da.Fill(dt1)
If dt1.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1
For j As Integer = 0 To dt1.Rows.Count - 1
If dt.Rows(i)("Text").ToString() = dt1.Rows(j)("SelectedText").ToString() Then
dt.Rows(i).Delete()
End If
Next
Next
End If
DropDownList1.DataSource = dt
DropDownList1.DataTextField = "Text"
DropDownList1.DataValueField = "Value"
DropDownList1.DataBind()
Session("ListItems") = dt
End If
End If
End Sub
Private Function DropDownItems() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Text")
dt.Columns.Add("Value")
dt.Rows.Add("Item1", "1")
dt.Rows.Add("Item2", "2")
dt.Rows.Add("Item3", "3")
dt.Rows.Add("Item4", "4")
Return dt
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = TryCast(Session("ListItems"), DataTable)
Dim liCol As ListItemCollection = DropDownList1.Items
For i As Integer = 0 To dt.Rows.Count - 1
Dim li As ListItem = liCol(i)
If li.Selected Then
dt.Rows(i).Delete()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO UserDetail VALUES(@SelectedText,@SelectedVal,@UserName)", con)
cmd.Parameters.AddWithValue("@SelectedText", li.Text)
cmd.Parameters.AddWithValue("@SelectedVal", li.Value)
cmd.Parameters.AddWithValue("@UserName", Session("UserName").ToString())
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
Next
Session("ListItem") = dt
DropDownList1.DataSource = dt
DropDownList1.DataTextField = "Text"
DropDownList1.DataValueField = "Value"
DropDownList1.DataBind()
End Sub