blah blah blah is here! blah blah » Close

up0down
link

I'm trying to create a login code and I'm not able to figureout the code, I'm just trying to compare the username & the password value from access table.

Usernames and passwords are in same access table, Table(User)>Column1(Users) and Column2(Pass). Using OleDB to create connection.

I want to do something like this:

for (int i = comboBox1.FirstIndex; i > comboBox1.TotalItems.Count; i++)
{
if (comboBox1.SelectedIndex == i && (textBox2.Text == DS.Tables[0].Rows[i][0].ToString()))
{
//Run a set of commands
this.Close();
return;
}
}


Any better ideas please???

last answered one year ago

16 answers

up1down
link

Your question's not very clear but I'd guess that all possible usernames are listed in comboBox1 in the same order as the records in the datatable and that a user is expected to select his/her name from the combobox and then enter the password in textBox2.

You then need to test whether the password entered corresponds with that user's password in the datatable.

If that's the case, then the following code should do it:

if (comboBox1.SelectedIndex > -1 && textBox2.Text == DS.Tables[0].Rows[comboBox1.SelectedIndex][1].ToString())
{
//Run a set of commands
this.Close();
return;
}

gsvirdi
412

Let me try this.... it had not worked still. Btw how can I get (fetch) the text value of the currently selected text in my comboBox1???? cdtext = comboBox1.SelectedItem.ToString();

vulpes
17279

Yes, that's the way to do it.

up0down
link

Bcoz commetns section does not support code tags so I'm posting this as an answer to make it clear.

What I've tried to do is that the background remains disabled till the active form is closed. But this combination is not working.... I'm having Form1, it opens LoginForm, successful log in open Editable form.

Code in Form1:
private LoginForm LF = null;
private Editables EF = null;
private void AdminLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Enabled = false;
LF = new LoginForm();
LF.FormClosed += LoginForm_FormClosed;
LF.Show();
}

private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
{
EF = new Editables();
EF.FormClosed += Editables_FormClosed;
LF = null;
this.WindowState = FormWindowState.Minimized;
}
private void Editables_FormClosed(object sender, FormClosedEventArgs e)
{
this.Enabled = true;
this.WindowState = FormWindowState.Normal;
EF = null;
}


There's no formclosing event added in Login Form.....
In present case the Form1 remains disabled even when Editables Form is closed....

vulpes
17279

You need to add EF.Show() in LoginForm_FormClosed. Apart from that, it should work.

up0down
link

Adding EF.Show(); LoginForm_FormClosed events breaks this code and Editables form does not opens. Okie leave it, I'm planning to drop this enabling thing as it's not so important.

Now another Important feature I had thought of is that, I can prompt a user about the documents which have to be released today in all the running Projects.
My xml is:
<cni>
<proj1>
<date></date>
</proj1>
<proj2>
<date></date>
</proj2>
<proj3>
<date></date>
</proj3>
</cni>

I've already populated all Proj tags in DropDownList & into DGV also

// Populating the comboBox
DataSet DS = new DataSet();
adapter.Fill(DS, "proj");
comboBox1.DataSource = DS.Tables[0];
comboBox1.DisplayMember = "proj";
comboBox1.ValueMember = "proj";
conn.Close();
cdtext = DS.Tables[0].Rows[comboBox1.SelectedIndex][0].ToString();
// Populating the DGV
DataSet ds = new DataSet();
ds.ReadXml(filename);
DataTable dt = ds.Tables[cdtext];


all I want is that I wish to check complete xml. Something like this:
if (((DateTime)row.Cells["Date"].Value).Date == DateTime.Today) then check
if (((DateTime)row.Cells["Date"].Value).Date == <date> field in xml)

Display the <Proj> tag. How to fetch this Proj in Complete xml?

up0down
link

Do you mean something like this ** ?

if (((DateTime)row.Cells["Date"].Value).Date == DateTime.Today) 
{
var projects = new List<string>();
foreach(DataTable table in ds.Tables)
{
DataRow dr = table.Rows[0];
DateTime date = DateTime.Parse(dr["date"].ToString()).Date;
if (date == DateTime.Today)
{
projects.Add(table.TableName);
}
}
string message = null;
if (projects.Count == 0)
{
message = "There are no projects with documents to be released today";
}
else if (projects.Count == 1)
{
message = "There is 1 project with documents to be released today, namely: \r\n\r\n\t" + projects[0];
}
else
{
message = "There are " + projects.Count + " projects with documents to be released today, namely: \r\n";
foreach(string project in projects)
{
message += "\r\n\t" + project;
}
}
MessageBox.Show(message);
}


** includes EDIT - see comments below

up0down
link

Gud Mrng from India

Hey vulpes, variable named projs have been used in this code, but have not be defined. Can we use the name of the table directly like this in the code? Kindly tell me what is this variable?

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + filename + ";Jet OLEDB:Database Password=SomeThinG");
conn.Open();
var comm = new OleDbCommand("SELECT proj FROM projs", conn);
var adapter = new OleDbDataAdapter(comm);
DataSet DS = new DataSet();
adapter.Fill(DS, "proj");
comboBox1.DataSource = DS.Tables[0];
comboBox1.DisplayMember = "proj";
comboBox1.ValueMember = "proj";
conn.Close();
cdtext = DS.Tables[0].Rows[comboBox1.SelectedIndex][0].ToString();

My mdb file contains a table named projs and that table contains proj column, which is having names of all projects in it.

vulpes
17279

Sorry, projs is a List<string>. I've edited the code to include its declaration. It shouldn't interfere with the projs table in the database but I've changed the name to projects to avoid any confusion.

up0down
link

Earlier I was trying to create it separately and call it in form_Load but now I've added it in form_load itself. still its not behaving like a good boy

DataSet ds = new DataSet();
ds.ReadXml(filename);
DataTable dt = ds.Tables[cdtext];
DataColumn dc = new DataColumn("Date", typeof(DateTime));
dt.Columns.Add(dc);
string format = "dd MMM yyyy";
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows['i']; // Symbol of ' is used so that this text should not become inclined.
// string s = dr["Date"].ToString().Trim() + " " + dr["Time"].ToString().Trim();
string s = dr["Rel_Date"].ToString().Trim();
DateTime temp;
DateTime.TryParseExact(s, format, null, 0, out temp);
dr["Date"] = temp;
}
dataGridView1.DataSource = ds;
dataGridView1.DataMember = cdtext;
dataGridView1.AutoResizeColumns();
DataGridViewColumn dgvc = dataGridView1.Columns["Date"];
dataGridView1.Sort(dgvc, ListSortDirection.Descending);
dgvc.Visible = false;

// Today's Deliverables
var projects = new List<string>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (((DateTime)row.Cells["Date"].Value).Date == DateTime.Today)
{
foreach (DataTable table in ds.Tables)
{
DataRow dr = table.Rows[0];
DateTime date = DateTime.Parse(dr["Rel_Date"].ToString()).Date;
if (date == DateTime.Today)
{
projects.Add(table.TableName);
}
}
string message = null;
if (projects.Count == 0)
{
message = "There are no projects with documents to be released today";
}
else if (projects.Count == 1)
{
message = "There is 1 project with documents to be released today, namely: \r\n\r\n\t" + projects[0];
}
else
{
message = "There are " + projects.Count + " projects with documents to be released today, namely: \r\n";
foreach (string project in projects)
{
message += "\r\n\t" + project;
}
}
MessageBox.Show(message, "Attention....", MessageBoxButtons.OK);
}
}

    Edit:

PS:
My xml file has two different projects with documents having today's date in them. So I should see names of Two projects in the popup message.

I just found out that: Proper Popup (with project name) is displayed ONLY if the xml/DGV's last row's Date is Today.
Otherwise the Projects.count remains Zero, and so the Popup for projects.count == 0 is displayed two times bcoz two projects are having Today's date in them.

vulpes
17279

If the message never appears then, on the face of it, there can't be a row in the DGV whose Date column contains today's date. In that event, the code would never enter the block of the outer 'if' statement.

vulpes
17279

I don't know what you're doing with this line: DataRow dr = dt.Rows['i']; but 'i' is a char literal which (in this scenario) is implicitly converted to an int with the same unicode value, namely 105. So, in other words, you'll always get the 106th row for every iteration of the for loop.

up0down
link

that DataRow dr = dt.Rows['i']; appeared logical to me..... U can find this in http://www.debugging.com/bug/23870 5th post... u've suggested this in a solution to sort the DGV where I was using one column as "Date" and another column as "Time". but I have to later change the appearance of my xml file and have to dropout the "Time" from it. It appeared logical that we are moving thru Row[0] to Row[dt.Rows.Count-1].... it's something different?

Okie let's say after finishing of that for loop if I again write something like DataRow DR = dt.Rows[0]; (Zero) then also it dosen't helps.

All Today's dates.....which exists before the last project in the xml are ignored. and the projects.Count remains zero. Whenever I select (comboBox1) that project which is having today's date... the message from projects.Count == 0 is displayed. But the row is highlighted to show that it's having today's date.

Confusing.... or maybe I'm not able to explain.

vulpes
17279

In the post you refer to the line was DataRow dr = dt.Rows[i] where i was the integer loop control variable which I'd have thought is what you want here. But, apart from that, the code looks OK to me and so I think the answer must lie with the XML file. Can you post the contents (or an extract) of this file so I can try it myself.

up0down
link

Here's the xml file.....

<?xml version="1.0" standalone="yes"?>
<CNI>
<gheco>
<Rel_Date>05 Jan 2010</Rel_Date>
<Rev>0</Rev>
<Doc>Some Document name</Doc>
</gheco>
<gheco>
<Rel_Date>06 Mar 2010</Rel_Date>
<Rev>P0</Rev>
<Doc>Some Document name</Doc>
</gheco>
<gheco>
<Rel_Date>06 Apr 2010</Rel_Date>
<Rev>P1</Rev>
<Doc>Some Document name</Doc>
</gheco>
<gheco>
<Rel_Date>21 May 2010</Rel_Date>
<Rev>0</Rev>
<Doc>Some Document name</Doc>
</gheco>
<gheco>
<Rel_Date>22 May 2010</Rel_Date>
<Rev>A</Rev>
<Doc>Some Document name</Doc>
</gheco>
<gheco>
<Rel_Date>25 May 2010</Rel_Date>
<Rev>A</Rev>
<Doc>Some Document name</Doc>
</gheco>
<GEPL>
<Rel_Date>06 Apr 2010</Rel_Date>
<Rev>0</Rev>
<Doc>Some Document name here</Doc>
</GEPL>
<JP>
<Rel_Date>15 May 2010</Rel_Date>
<Rev>0</Rev>
<Doc>Some Document name</Doc>
</JP>
<JP>
<Rel_Date>16 May 2010</Rel_Date>
<Rev>P0</Rev>
<Doc>Some Document name</Doc>
</JP>
<Rajpura>
<Rel_Date>17 May 2010</Rel_Date>
<Rev>A</Rev>
<Doc>Some Document name</Doc>
</Rajpura>
<Rajpura>
<Rel_Date>25 May 2010</Rel_Date>
<Rev>P1</Rev>
<Doc>Some Document name</Doc>
</Rajpura>
<Jhajjar>
<Rel_Date>10 May 2010</Rel_Date>
<Rev>P0</Rev>
<Doc>Some Document name</Doc>
</Jhajjar>
</CNI>

up0down
link

I think I'm now beginning to see the wood from the trees!

In sorting out today's deliverables, you don't want to be looking at the DGV at all (which only displays the rows of a single table). You simply need to be iterating through the tables themselves and determining those which have rows whose Rel_Date column shows today's date.

So, the revised code will now be:

DataSet ds = new DataSet();
ds.ReadXml(filename);
DataTable dt = ds.Tables[cdtext];
DataColumn dc = new DataColumn("Date", typeof(DateTime));
dt.Columns.Add(dc);
string format = "dd MMM yyyy";
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
string s = dr["Rel_Date"].ToString().Trim();
DateTime temp;
DateTime.TryParseExact(s, format, null, 0, out temp);
dr["Date"] = temp;
}
dataGridView1.DataSource = ds;
dataGridView1.DataMember = cdtext;
dataGridView1.AutoResizeColumns();
DataGridViewColumn dgvc = dataGridView1.Columns["Date"];
dataGridView1.Sort(dgvc, ListSortDirection.Descending);
dgvc.Visible = false;

// Today's Deliverables
var projects = new List<string>();
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
DateTime date = DateTime.Parse(dr["Rel_Date"].ToString()).Date;
if (date == DateTime.Today)
{
projects.Add(table.TableName);
continue; // no need to check any more rows
}
}
}
string message = null;
if (projects.Count == 0)
{
message = "There are no projects with documents to be released today";
}
else if (projects.Count == 1)
{
message = "There is 1 project with documents to be released today, namely: \r\n\r\n\t" + projects[0];
}
else
{
message = "There are " + projects.Count + " projects with documents to be released today, namely: \r\n";
foreach (string project in projects)
{
message += "\r\n\t" + project;
}
}
MessageBox.Show(message, "Attention....", MessageBoxButtons.OK);

up0down
link

BINGO!!!!! Man u did it again.....

Figuring out these logics is a tough thing for me.... As u now know that the comboBox1 in my form fetches the list of projects, that name of the project in comboBox1 is same as that in the xml file. So when the user selects a particular project, that name is sent as DataTable dt = ds.Tables[cdtext]; and rows of THAT project only is populated & displayed in my DGV. Now just see this code below....

I'm simply trying to export my DGV which is visible to the user, using Office 11.0 Object Library... using Excel = Microsoft.Office.Interop.Excel;
I'm planning to do the formatting of my Excel document. Needs help with formatting, like:

  • Background coloring of cells having HeaderText

  • Auto Adjusting the width of all columns (fit text)

  • Justification of text in individual Column

  • Excel.ApplicationClass excelSheet = new Excel.ApplicationClass();
    excelSheet.Application.Workbooks.Add(Type.Missing);
    excelSheet.Columns.ColumnWidth = 10;
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
    excelSheet.Cells[1, column.Index + 1] = column.HeaderText;
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Font.Bold = true;
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Font.Color = 10;
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Borders.Value = 1;
    }
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    foreach (DataGridViewCell cell in row.Cells)
    {
    excelSheet.Cells[cell.RowIndex + 2, cell.ColumnIndex + 1] = cell.Value.ToString();
    ((Excel.Range)excelSheet.Cells[cell.RowIndex + 2, cell.ColumnIndex + 1]).Borders.Value = 1;
    }
    ((Excel.Range)excelSheet.Columns[1, Type.Missing]).NumberFormat = "dd-mmm-yy";
    ((Excel.Range)excelSheet.Columns[4, Type.Missing]).Delete(4);
    }
    excelSheet.ActiveWorkbook.SaveCopyAs(exportfile);
    excelSheet.ActiveWorkbook.Saved = true;
    excelSheet.Quit();
    excelSheet.Application.Quit();
    if (File.Exists(exportfile))
    MessageBox.Show("Data Exported", "Done");
    else
    MessageBox.Show("I'm sorry, something unpleasant happened", "Opps.....");



    Plz try and see....

    up0down
    link

    I'm not too hot on programatically formatting stuff in Excel but I have managed to figure out how to deal with those three requirements:

    Excel.ApplicationClass excelSheet = new Excel.ApplicationClass();
    excelSheet.Application.Workbooks.Add(Type.Missing);

    //excelSheet.Columns.ColumnWidth = 10; // not needed
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
    excelSheet.Cells[1, column.Index + 1] = column.HeaderText;
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Font.Bold = true;
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Font.Color = 10;
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Borders.Value = 1;
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Interior.ColorIndex = 8; // cyan
    }

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    foreach (DataGridViewCell cell in row.Cells)
    {
    if (cell.Value != null) // make sure value isn't null
    {
    excelSheet.Cells[cell.RowIndex + 2, cell.ColumnIndex + 1] = cell.Value.ToString();
    }
    ((Excel.Range)excelSheet.Cells[cell.RowIndex + 2, cell.ColumnIndex + 1]).Borders.Value = 1;
    }
    ((Excel.Range)excelSheet.Columns[1, Type.Missing]).NumberFormat = "dd-mmm-yy";
    ((Excel.Range)excelSheet.Columns[4, Type.Missing]).Delete(4);
    }

    excelSheet.Columns.AutoFit(); // autofit columns after adding data
    excelSheet.Columns.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft; // left justify all columns

    excelSheet.ActiveWorkbook.SaveCopyAs(exportfile);
    excelSheet.ActiveWorkbook.Saved = true;
    excelSheet.Quit();
    excelSheet.Application.Quit();
    if (File.Exists(exportfile))
    MessageBox.Show("Data Exported", "Done");
    else
    MessageBox.Show("I'm sorry, something unpleasant happened", "Opps.....");

    There's a table of Excel colors and their indices here.

    up0down
    link

    The line to give color to font is corrected as this:
    ((Excel.Range)excelSheet.Cells[1, column.Index + 1]).Font.ColorIndex = 10; otherwise it is not working, The font color remains Black only.

    Now....

    Just a doubt.... can we change the name of Sheet1 to something else and remove remaining Sheet2 & Sheet3 also????

    up0down
    link

    To do that change the first two lines to the following:

    Excel.ApplicationClass excelSheet = new Excel.ApplicationClass();                 
    Excel.Workbook workbook = excelSheet.Application.Workbooks.Add(Type.Missing);
    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
    worksheet.Name = "GSVirdi"; // or whatever
    ((Excel.Worksheet)workbook.Worksheets[3]).Delete();
    ((Excel.Worksheet)workbook.Worksheets[2]).Delete();


    EDIT - see comments below

    The Office object model was originally designed for VBA/COM programmers and has always been pretty horrible for C# folks to use with its liberal use of 'variant' return types, indexed properties and optional parameters. Many C# developers just gave up and used VB.Net instead which does support all this stuff.

    However, muster is right - MSDN does have the best articles on the subject - and I'd check out this one in particular and also some of the others listed in the TreeView to the left of that page.

    Incidentally, if you upgrade to VS 2010, you'll find that Office interop has suddenly become much nicer now that C# supports the 'dynamic' keyword and optional/named parameters. It also supports indexed properties but only for Office/COM interop.

    It's even possible now for your application to extract only those bits of the Primary Interop Assemblies (PIAs) that it needs which improves performance and makes deployment much easier :)

    up0down
    link

    Thx..... That's wonderful piece of help.

    I might have exported a nice looking xls file with ur help... But I'm not satisfied with the amount of learning in Office.Interop thing... sounds too tricky and confusing. Can u please help me out with a good tutorial for a newbie(in Office.Interop)? Would be wonderful....

    I want to have a better look into this thing otherwise it feels as if I'm trying-out with some alien language.

    In the meanwhile I'm also trying to search something. I felt that it would be great if I can do something easy so that my DGV can display the xls file without much doing finguring in my DGV. So I need some help/tutorial for Office.Interop......

    muster
    1556

    hey, you can still learn more about Office.Interop by going to the msdn website and it will be your sufficient reference afaik.

    vulpes
    17279

    I've tried twice now to add another answer but both attempts have disappeared into thin air even though the answer count was incremented! I've therefore edited my last answer instead.

    Feedback