How do I get role name for user in dnn? - c#

I am new programmer in DNN. My question is How do I get role name for user in DNN?
My code is:
protected void Page_Load(object sender, EventArgs e)
{
int UserId = UserController.GetCurrentUserInfo().UserID;
int PortalId = PortalSettings.PortalAlias.PortalID;
UserInfo user = UserController.GetUserById(PortalId, UserId);
String[] array = new String[5];
for (int x = 0; x < 5; x++)
{
array[x] = UserInfo.Roles.ToString();
}
TextBox1.Text = array[0];
TextBox2.Text = array[1];
}
But just display System.String[] in textboxes.
What should i do?

You can use this code:
String[] array = new String[5];
var user = UserController.GetUserByName(UserInfo.Username);
for (int x = 0; x < user.Roles.Count(); x++)
{
array[x] = user.Roles[x];
}

Related

Loop of creation via a user input, WPF C#

The system needs a way of taking input from txtBox.
I then need the system to take the number input, 1-10. And repeat the ticket and file creation for that x amount of times.
This code also creates a file on the pc with the Lottery ticket in it.
the files have to be named LottoKupon1, LottoKupon2 and so on, this needs to match user input of course.
this is for creation of file and folder
namespace Lottery
{
internal class FolderCreater
{
static string folderName = #"C:\lotto";
public static string fileName = #"C:\lotto\LottoKupon.txt";
public static void CreateFolder()
{
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(folderName);
}
else if (!File.Exists(fileName))
{
File.Create(fileName);
}
else { }
}
}
}
This is for creation of Lottry Ticket
namespace Lottery
{
// the system needs a way of taking input
// from txtBox, int user = int.Parse(txtBox.Text.Trim()); which this line does
// i then need the system to take the number input, 1-10.
// and repeat the ticket and file creation for that x amount of times.
public partial class MainWindow : Window
{
public List<long> ListOfAllArrays = new List<long>();
int i = 1;
Random randNum = new Random();
public MainWindow()
{
InitializeComponent();
}
public void Lotto ()
{
StreamWriter sw = new StreamWriter(FolderCreater.fileName);
sw.WriteLine(" " + "Lotto " + DateTime.Now.ToString("dd/MM/yyyy\n"));
sw.WriteLine(" " + "1-uge\n" + " LYN-LOTTO\n");
Random randNum = new Random();
int[][] AllArrays = new int[10][];
for (int i = 0; i < 10; i++)
{
AllArrays[i] = new int[7];
}
int RowNr = 0;
foreach (int[] row in AllArrays)
{
RowNr++;
sw.Write(RowNr + ". ");
for (int j = 0; j < 7; j++)
{
int number = randNum.Next(1, 37);
while (row.Any(n => n == number))
{
number = randNum.Next(1, 37);
}
row[j] = number;
}
Array.Sort(row);
for(int l = 0; l < 7; l++)
{
sw.Write(row[l].ToString("00") + " ");
}
sw.Write("\n");
}
TestIfSame(AllArrays);
if (CheckBox.IsChecked == true)
{
sw.WriteLine("\n****** Joker Tal ******");
int[][] AllJokerRows = new int[2][];
for (int i = 0; i < 2; i++)
{
AllJokerRows[i] = new int[7];
}
foreach (int[] n in AllJokerRows)
{
sw.Write(" ");
for (int i = 0; i < 7; i++)
{
var JokerNumber = randNum.Next(1, 10);
n[i] = JokerNumber;
}
Array.Sort(n);
for (int u = 0; u < 7; u++)
{
sw.Write(n[u] + " ");
}
sw.Write("\n");
}
}
sw.Close();
}
public void TestIfSame(int[][] AllArrays)
{
//List<long> ListOfAllArrays = new List<long>();
for (int i = 0; i < AllArrays.Length; i++)
{
ListOfAllArrays.Add(MakesLongNumber(AllArrays[i]));
}
for (int i = 0; i < AllArrays.Length; i++)
{
long temp = ListOfAllArrays[0];
ListOfAllArrays.Remove(ListOfAllArrays[0]);
if (ListOfAllArrays.Contains(MakesLongNumber(AllArrays[i])))
{
Lotto();
}
else
{
ListOfAllArrays.Add(temp);
}
}
long MakesLongNumber(int[] row)
{
string a = "";
for (int i = 0; i < row.Length; i++)
{
a += row[i].ToString();
}
return Convert.ToInt64(a);
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
}
private void cmdOk_Click_1(object sender, RoutedEventArgs e)
{
FolderCreater.CreateFolder();
Lotto();
// add a ListBox.Items.Add(); within this needs to be the command for the Lottery ticket, so i can have it shown on a list box.
}
**This is for a Lottery assignment, its on a lower level of coding, so no answer that is way too complicated, hope you understand. please ask questions if needed.
I tried a dew things with a for loop around the first void which took user input, but i could not make it work.
the code is expected to create files and Lottery ticket depending on users input**

Arrays not averaging out correctly using a loop

namespace Files_and_Arrays_II
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
StreamReader inputFile;
int doctor = 0;
double total = 0, average_sys = 0;
string name, DocN;
string[] doctors = new string[3] { "D. ABRAMS, MD", "D. JARVIC, MD", "T. PANOS, MD" };
int[] systolic = new int[5];
int[] diastolic = new int[5];
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
inputFile = File.OpenText(openFile.FileName);
while (!inputFile.EndOfStream)
{
name = inputFile.ReadLine();
for (int i = 0; i < 5; i++)
{
systolic[i] = int.Parse(inputFile.ReadLine());
diastolic[i] = int.Parse(inputFile.ReadLine());
}
//Calculates average for systolic
for (int count = 0; count < systolic.Length; count++)
{
total += systolic[count];
}
average_sys = total / 5;
doctor = int.Parse(inputFile.ReadLine());
DocN = doctors[doctor];
listBox1.Items.Add(name + "\t" + average_sys + "\t" + DocN);
}
}
}
}
}
This is the file it is getting it from
When running the program I get the following averages for systolic: 184.6 (correct), 312 (wrong).
I've tried resetting the array at the end of the loop but that solves nothing
Others have pointed out the problem in this case, but it's a symptom of declaring variables at the top of the function. If you'd declared them close to where they are used, it would be obvious which variables apply to the whole function and which have a scope that only applies inside the loop.
Like this:
string name = inputFile.ReadLine();
//Calculates average for systolic
double total = 0;
for (int count = 0; count < systolic.Length; count++)
{
total += systolic[count];
}
double average_sys = total / 5;
int doctor = int.Parse(inputFile.ReadLine());
string DocN = doctors[doctor];
listBox1.Items.Add(name + "\t" + average_sys + "\t" + DocN);
Even better, use var instead of setting the variable type in two places and risking getting it wrong.
You're not resetting your variables for total and average for the second set of measurements.

Read File and store into array and display sum in textbox

I'm trying to get data from a file and store them in an array then display the data in a listbox the find then sum and display it in a text box. Here's my code and it doesn't work. I'm not sure what i'm doing wrong.
private void findClick(object sender, EventArgs e)
{
int sum;
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader InputFile = new StreamReader(ofd.FileName))
{
while (InputFile.EndOfStream == false)
{
int[] array = new int[listBox.Items.Count];
for (int i = 0; i < listBox.Items.Count; i++)
{
// array[i] = Convert.ToInt32(listBox.Items[i].ToString());
array[i] = int.Parse(listBox.Items[i].ToString());
sum = array.Sum();
TotalAmtlabel.Text = sum.ToString("N0");
TotalNumberslabel.Text = listBox.Items.Count.ToString();
TotalAmountlabel.Text = string.Format("{0:N0}", sum);
}
}
}
}
}
}
listBox.Items.AddRange(File.ReadAllLines(ofd.FileName));
Try this and modify according to your needs:
string[] amounts = File.ReadAllLines(ofd.FileName);
int currentSum = 0;
int totalSum = 0;
ListItem[] amountItems = new ListItem[amounts.Length];
for (int i = 0; i < amounts.Length; i++)
{
if (int.TryParse(amounts[i], out currentSum))
{
totalSum += currentSum;
}
amountItems[i] = amounts[i];
}
listBox.Items.AddRange(amountItems);
TotalAmountlabel.Text = string.Format("{0}", totalSum);
You can also datasource to bind the list. Please go through below MSDN references once atleast to understand security cautions:
ListItem
ListBox

Need to Scramble the selected Text

protected void Button_Upload_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/Data/" + FileUpload1.FileName);
string[] readtext = File.ReadAllLines(path);
var a = readtext;
List<string> strList = new List<string>();
foreach (string s in readtext)
{
strList.Add(s);
}
ListBox1.DataSource = strList;
ListBox1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
var b = ListBox1.SelectedIndex;
var a = ListBox1.SelectedValue.ToString();
if (b < 0)
{
// no ListBox item selected;
return;
}
StringBuilder jumbleSB = new StringBuilder();
jumbleSB.Append(a);
Random rand = new Random();
int lengthSB = jumbleSB.Length;
for (int i = 0; i < lengthSB; ++i)
{
int index1 = (rand.Next() % lengthSB);
int index2 = (rand.Next() % lengthSB);
Char temp = jumbleSB[index1];
jumbleSB[index1] = jumbleSB[index2];
jumbleSB[index2] = temp;
}
Console.WriteLine(jumbleSB);
TextBox1.Text = ListBox1.Text.ToString();
//TextBox1.Text = ListBox1.Text.Insert(jumbleSB);
Here I need to Jumble the values which selected by User. When User selects a Value it has to jumble and has to come to Textbox. I am not able to displaying the Jumble values. Any help Please...??
Console.WriteLine(jumbleSB.ToString());

How Can i get Two column winform listbox in C#?

I am doing Student Attendance project for college in win form with MySQL(C#).
In that I want to move data one listbox to another listbox. I done that. But i load the student name in that. Now Client Want Name and adminno like datagridview Columns.
I search for this,. Vb has this type of coding. See Multi Column Listbox. Is it Possible in C#?.
See the Below Picture. Its my Form.,..
My Code For Loading Listbox
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select name,admin_no from student_admision_master where course='" + course_code + "' AND year='" + year_code + "' AND sem='" + semester_code + "' AND batch='" + batch_code + "'";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
listBox1.Items.Add(Reader[0].ToString() + "," + Reader[1].ToString());
}
connection.Close();
This give the result jagadees, 125445.
But want to disparate Separate Columns.
My Code For Moving Data's
private void btn_toAb_Click_Click(object sender, EventArgs e)
{
int count = listBox1.Items.Count;
for (int i = 0; i < count; i++)
{
listBox2.Items.Add(listBox1.Items[i].ToString());
}
listBox1.Items.Clear();
}
private void btn_fromAb_Click_Click(object sender, EventArgs e)
{
int count = listBox2.Items.Count;
for (int i = 0; i < count; i++)
{
listBox1.Items.Add(listBox2.Items[i].ToString());
}
listBox2.Items.Clear();
}
private void btn_toAb_Selected_Click(object sender, EventArgs e)
{
int count = listBox1.SelectedItems.Count;
for (int i = 0; i < count; i++)
{
listBox2.Items.Add(listBox1.SelectedItems[i].ToString());
}
for (int i = 0; i < count; i++)
{
listBox1.Items.Remove(listBox1.SelectedItems[0]);
//listBox1.add
}
}
private void btn_fromAb_Selected_Click(object sender, EventArgs e)
{
int count = listBox2.SelectedItems.Count;
for (int i = 0; i < count; i++)
{
listBox1.Items.Add(listBox2.SelectedItems[i].ToString());
}
for (int i = 0; i < count; i++)
{
listBox2.Items.Remove(listBox2.SelectedItems[0]);
}
}
Thanks in advance!...
The Windows Forms Control "ListView" can do that.
You can do it by using String.Format() method. Inside is each method yoz define the alignment of each item in a row.
I dont have a db, but instead in my example I use two array (same as your reader[0] and reader[1] indexers). So Instead of array1[i], and array2[i] you use Reader[0] and Reader[1].
Here is the exampe:
string[] array1 = { "name 1", "name10", "name104", "name 222", "name 3212" };
string[] array2 = { "12343", "23", "432", "4333", "1" };
const int a = 40;
int b = 0;
for (int i = 0; i < array1.Length; i++)
{
if (array1[i].Contains(' '))
b = array1[i].Length - 1;
else
b = array1[i].Length;
b = -(a - b);
listBox1.Items.Add(String.Format("{0," + b + "} {1}", array1[i], array2[i]));
}

Categories

Resources