Hi selverca,
Please refer below sample.
SQL
CREATE TABLE [Projects]
(
[ID] INT IDENTITY,
[Prosjektnummer] VARCHAR (20),
[aktiv] VARCHAR (20),
[PI_bnava] VARCHAR (20),
)
INSERT INTO [Projects] VALUES('123','False','User1')
INSERT INTO [Projects] VALUES('789','False','User5')
INSERT INTO [Projects] VALUES('456','False','User1')
INSERT INTO [Projects] VALUES('123','False','User1')
SELECT * FROM [Projects]
DROP TABLE [Projects]
HTML
<table>
<tr>
<td>UserName:</td>
<td>
<asp:TextBox ID="txtusername" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnlogin" Text="Login" runat="server" OnClick="Login" />
</td>
</tr>
</table>
<asp:Repeater ID="rptDetails" runat="server">
<HeaderTemplate>
<table>
<tr>
<td></td>
<td>Id</td>
<td>Proshektnummer</td>
<td>Prosjektnavn</td>
<td>prosjektbeskrivelse</td>
<td></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="chkBox" runat="server" /></td>
<td>
<asp:Label ID="lblId" Text='<%#Eval("Id") %>' runat="server" />
</td>
<td>
<asp:Label ID="lblProshektnummer" Text='<%#Eval("Proshektnummer") %>' runat="server" />
</td>
<td>
<asp:Label ID="lblProsjektnavn" Text='<%#Eval("Prosjektnavn") %>' runat="server" />
</td>
<td>
<asp:Label ID="lblprosjektbeskrivelse" Text='<%#Eval("prosjektbeskrivelse") %>' runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Login(object sender, EventArgs e)
{
string loginUser = txtusername.Text;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("Id"),
new DataColumn("Proshektnummer"),
new DataColumn("Prosjektnavn"),
new DataColumn("prosjektbeskrivelse")});
dt.Rows.Add("1", "123", "Testing1", "Database");
dt.Rows.Add("2", "456", "Testing2", "Database");
dt.Rows.Add("3", "789", "Testing3", "Database");
dt.Rows.Add("4", "555", "Testing4", "Database");
rptDetails.DataSource = dt;
rptDetails.DataBind();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Projects", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt1 = new DataTable();
sda.Fill(dt1);
DataRow[] dr = dt1.Select("PI_bnava='" + loginUser + "'");
foreach (RepeaterItem item in rptDetails.Items)
{
Label pNo = item.FindControl("lblProshektnummer") as Label;
CheckBox isLogin = item.FindControl("chkBox") as CheckBox;
List<DataRow> result = dr.AsEnumerable().Where(x => x["Prosjektnummer"].ToString() == pNo.Text).ToList();
isLogin.Checked = result.Count > 0;
}
}
}
}
}
VB.Net
Protected Sub Login(ByVal sender As Object, ByVal e As EventArgs)
Dim loginUser As String = txtusername.Text
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Id"),
New DataColumn("Proshektnummer"),
New DataColumn("Prosjektnavn"),
New DataColumn("prosjektbeskrivelse")})
dt.Rows.Add("1", "123", "Testing1", "Database")
dt.Rows.Add("2", "456", "Testing2", "Database")
dt.Rows.Add("3", "789", "Testing3", "Database")
dt.Rows.Add("4", "555", "Testing4", "Database")
rptDetails.DataSource = dt
rptDetails.DataBind()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Projects", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt1 As DataTable = New DataTable()
sda.Fill(dt1)
Dim dr As DataRow() = dt1.[Select]("PI_bnava='" & loginUser & "'")
For Each item As RepeaterItem In rptDetails.Items
Dim pNo As Label = TryCast(item.FindControl("lblProshektnummer"), Label)
Dim isLogin As CheckBox = TryCast(item.FindControl("chkBox"), CheckBox)
Dim result As List(Of DataRow) = dr.AsEnumerable().Where(Function(x) x("Prosjektnummer").ToString() = pNo.Text).ToList()
isLogin.Checked = result.Count > 0
Next
End Using
End Using
End Using
End Sub
Screenshot