|
Wow, there's a lof of stuff bad with this. OK.
Without doing anything to your database, which itself needs a redesign, you can build an SQL SELECT with and IN clause.
First, you don't need all the month* variables. Get rid of them.
Next, add a Tag value to each of your CheckBox's with the month name corresponding to each one. CheckBox1.Tag="'January'", CheckBox2.Tag="'Febuary'", ... (NOTE: The single quotes in those strings!)
Wire up all the CheckBox's CheckChanged events to the same handler. See the Handles clause at the end?? Add all of the CheckBox's to it.
Private Sub CheckBox_CheckChanged(blah, blah) Handles, CheckBox1.CheckChanged, CheckBox2.CheckChanged, ...
In this handler, cast the sender back to a CheckBox and get the Tag property from it. In an ArrayList collection, check to see if the Tag value is in the collection, if not, Remove it, else, Add it. A quick'n'dirty version of this would look something like:
Dim cb As CheckBox = DirectCast(sender, CheckBox)
If Not myMonthList.Contains(cb.Tag) Then
myMonthList.Add(cb.Tag)
Else
myMonthList.Remove(cb.Tag)
End If
Mind you, this code doesn't care if the checkbox is checked or not!! You might want to factor that into this code.
When you go to build the SQL query string, you'turn the collection of months, myMonthList in the above example, into a String of values seperated by commas, like this:
Dim months As String = String.Join(",", myMonthList.ToArray(GetType(String)))
You then make that list part of you SQL statement:
Dim sql As String = String.Format("SELECT * FROM Finacial_Request_Report WHERE year1='{0}' AND month1 IN ({1})", NumericUpDown1.Text, months)
You'll get a statement that looks something like this:
SELECT * FROM Financial_Request_Report WHERE year1=1980 AND month1 IN ('January','Febuary')
But, this is quick'n'dirty. A good solution would require you to redesign your database and scrap all the code you put up in your original post.
|
|
|
|
|
hii
i am able to draw a rectangle of any size , Now i want to draw small size of rectangles in big rectangle , so that user can align the controls.
And i want the big rectangle as dotted line . ( For example in vb .net show grid options ) in that they showed as dots) , but i want to show in rectangles . For example , Like Graph sheet
DVSRIRAM
|
|
|
|
|
All you have to do is draw a line from the top of the rectangle to the bottom, every so often acrossed the rectangle. Then repeat the process drawing lines across the rectangle.
You already have the position and dimensions of the rectangle. The rest is easy. Don't even think of copy'n'pasting this into your code, cause it's not real code. You actually have to write it yourself.
'Draw your rectangle, then do something like this:
For x = 0 to rectangleWidth Step spacing
DrawLine( x+rectangleLeft, 0, x+rectangleLeft, rectangleHeight )
Next
For x = 0 to rectangleHeight Step spacing
DrawLine( 0, x+rectangleTop, rectangleWidth, x+rectangleHeight )
Next
|
|
|
|
|
I have a simple vb.net app that takes a user's username and password (they have to enter it in) and it will open a program using a RunAs function. What I need to check before hand is if the credentials that I have been given have administrative rights on the workstation it is attempting to log in with. The person logging into the app may not be the same credentials that logged into the PC. If they don't have admin rights I need to keep them from logging in a presetn them a message. Does anyone have a code example of this process?
Thanks,
archangel_26
|
|
|
|
|
It's amazing what you can find if you just type in a little phrase into Google. Like trying "VB.NET check for admin[^]".
|
|
|
|
|
Yes I did already check that - I already found those examples and neither of them would work for me. I will freely admit to being a VB.net newbie. So, do you have any suggestions?
|
|
|
|
|
Neither?? That implies two. There's hundreds of them...
OK. There's a bit of a problem with checking to see if a user has admin rights. You can't just check on the local machine, and find out "OK this person is good". You actually have to ask the remote machine your connecting to if the person has admin rights. But, there's no function to do that. The best way to find out is actually attempt to do whatever it is your doing, and if it fails, then you know.
|
|
|
|
|
Maybe this is something for you, it costed me blood and tears to get it right
Copy the code below in a function, you still need to change the static values DOMAIN and USER to dynamic values
DOMAIN can be ComputerName or DomainName
USER is the user account used for to logon
Don't forget to put the following on top of your page:
Imports System.Management
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_GroupUser WHERE PartComponent = " & Chr(34) & "Win32_UserAccount.Domain=\" & Chr(34) & "DOMAIN\" & Chr(34) & ",Name=\" & Chr(34) & "USER\" & Chr(34) & Chr(34))
For Each queryObj As ManagementObject In searcher.Get()
Console.WriteLine("-----------------------------------")
Console.WriteLine("Win32_GroupUser instance")
Console.WriteLine("-----------------------------------")
Console.WriteLine("GroupComponent: {0}", queryObj("GroupComponent"))
Console.WriteLine("PartComponent: {0}", queryObj("PartComponent"))
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
The result of this query is:
-----------------------------------
Win32_GroupUser instance
-----------------------------------
GroupComponent: \\NC8000\root\cimv2:Win32_Group.Domain="DOMAIN",Name="Administrators"
PartComponent: \\NC8000\root\cimv2:Win32_UserAccount.Domain="DOMAIN",Name="USER"
-----------------------------------
Win32_GroupUser instance
-----------------------------------
GroupComponent: \\NC8000\root\cimv2:Win32_Group.Domain="DOMAIN",Name="Debugger Users"
PartComponent: \\NC8000\root\cimv2:Win32_UserAccount.Domain="DOMAIN",Name="USER"
good luck
|
|
|
|
|
This is nice and all, but it only checks group membership. It doesn't check anything against the the resultant set of policies and security permissions.
|
|
|
|
|
I've found a bit of code that almost works. Here is the scenario:
A set of users will belong to a group. It is called "group1" . I am attempting to run this code to check if they belong to this group. If so - let them continue. If not, then catch the error. Here is the problem. When I (myaccount) is logged into the machine it will authenticates fine for me. It won't authenticate for someone who is a member of Group1. Even if the other user is a member of group1 and has admin rights such as "Otheraccount" does below. The workstation this is running on has this in the Administrators group:
DOMAIN\myaccount
DOMAIN\otheraccount
DOMAIN\Group1 (myaccount and otheraccount are members of this group)
I am running this code:
'***********************************************************
Dim SecurityCheck As New PrincipalPermission("DOMAIN\"& strUserName, "DOMAIN\Group1")
Dim strError As String
Try
SecurityCheck.Demand()
Catch errormessage As System.Exception
strError = errormessage.Message
If strError = "Request for principal permission failed." Then
MsgBox("You are not a local administrator. You do not have rights to access this software. If you need access, please contact your manager.")
Me.Close
Exit sub
Else
msgbox(strError)
End If
'***********************************************************
Any suggestions? I have tried this as well with the same results:
Dim SecurityCheck As New PrincipalPermission("DOMAIN\"& strUserName, "Administrators")
|
|
|
|
|
archangel_26 wrote: I've found a bit of code that almost works.
Not "almost". Actually, it'll never work.
To check the user currently running the code against the role, you have to use an IPrincipal object representing the user, not a PrincipalPermission. You created a Permission object, not a Principal object you can check roles against.
' Get the current prinicpal this thread is running under
Dim userPrincipal As IPrincipal = System.Threading.Thread.CurrentPrincipal
' Check if this principal is a member, through any number of nested groups
' and permissions, of the local Administrators group. For instance, if in
' a domain environment, a user can be a member of the Domain Admins domain
' group, which in turn can be a member of the local machines Administrators
' group. This line would return true if that user was running this code.
Dim result As Boolean = userPrincipal.IsInRole("Administrators")
|
|
|
|
|
Dave,
I see how that can work for the logged in user:
'****************************************************************************
Dim userPrincipal As IPrincipal = System.Threading.Thread.CurrentPrincipal
userPrincipal = Thread.CurrentPrincipal
Dim result As Boolean = userPrincipal.IsInRole("DOMAIN\Group1")
msgbox(result)
'****************************************************************************
Is there any way I can specify the NAME I want it to check? The below code will not work because the property NAME is readonly:
'****************************************************************************
Dim userPrincipal As IPrincipal = System.Threading.Thread.CurrentPrincipal
userPrincipal.Identity.Name = "DOMAIN\"& strUserName
Dim result As Boolean = userPrincipal.IsInRole("DOMAIN\Group1")
msgbox(result)
'****************************************************************************
Is there any way I can change the NAME I want it to check? At this point in the process, the strUserName I want to check against I get from a text field on a form the user has filled out.
|
|
|
|
|
The onyl way to do that would be to log the person in, requiring you get the users password and create a security token impersonating them. You can't just pass a username and a group and call it done.
Personally, I avoid such checks as it requires the users to, yet again, type another username and password, even if it's the same as their login account. It's far easier to check the Principal object that's currently running the code.
|
|
|
|
|
Actually they need to enter in their credentials. I'm creating this small .exe for a non-secure application. This application is installed on workstations that are located in an open area. The account these machines are logged into with is a "common" account. Since the software does not do domain authentication or ask for a log in, we are trying this to get it secure.
We want people to run this small .exe as a "login" for the app. They enter their network userid and password. I want to run the admin rights check we've been discussing. Once they pass the check, I execute a "Run As" statement that uses their entered credentials. Since I need to collect the userid and password for the Run As, this security token you are referring to may help. How do you create a security token for impersonating them?
|
|
|
|
|
So why not just ask for the cred's, launch the app RunAs, and if that fails, you know the cred's were bad, and in the Admin app, do the check for Admin rights?
|
|
|
|
|
Thats just it - ANYONE can run the vendor application right now. If you log into the workstation as the local administrator or a guest user, you can get into the application. We initially started this .exe as a way to log who is accessing the program, then someone asked to add a check to see if the network account belonged to a domain group as a form of security.
|
|
|
|
|
OK. I thought you were writing this other app.
This is NOT easy to do. You also will not shell out to RunAs to launch the other process. Use the Process and ProcessStartInfo classes. You have to supply the username, password, and domain name that the user is in to ProcessStartInfo so it can launch the process under the correct account.
But, to find out if the person is using the correct credentials and is in the Administrators group:
Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.Runtime.InteropServices
Imports System.ComponentModel
.
.
.
Private Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Private Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Private Declare Auto Function LogonUser Lib "advapi32.dll" ( _
ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) _
As Boolean
Private Declare Auto Function CloseHandle Lib "kernel32.dll" ( _
ByVal handle As IntPtr) _
As Boolean
Private Function IsUserAnAdmin(ByVal username As String, ByVal password As String, ByVal domain As String) As Boolean
Dim returnValue As Boolean = False
Dim tokenHandle As New IntPtr(0)
' Call the Win32 LogonUser function to get a security token.
' If the call succeeds, LogonReturn will be True. In the credentials supplied
' are bad, this will return False.
Dim LogonReturn As Boolean
LogonReturn = LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, _
LOGON32_PROVIDER_DEFAULT, tokenHandle)
Try
If False = LogonReturn Then
' If the Logon failed, you can either throw an exception back to the caller
' or just set returnValue to False. This code does both for no good reason.
Dim errorCode As Integer = Marshal.GetLastWin32Error()
Throw New Win32Exception(errorCode)
returnValue = False
Else
' LogonUser worked! User the returned security token to create a new
' WindowsIdentity object...
Dim newIdentity As New WindowsIdentity(tokenHandle)
' Then create an impersonation context out of that so we can revert
' back to being our normal logged in user...
Dim impersonatedUser As WindowsImpersonationContext = newIdentity.Impersonate()
' The create a new Principal object so we can check role membership...
Dim newPrincipal As New WindowsPrincipal(newIdentity)
' Is the new user an Admin???
returnValue = newPrincipal.IsInRole("Administrators")
' Forget we logged in as this new person and revert back to
' the original user.
impersonatedUser.Undo()
CloseHandle(tokenHandle)
End If
End Try
' Tell the caller what happened...
Return returnValue
End Function
|
|
|
|
|
That worked perfectly! Thank you for all your help.
|
|
|
|
|
plz tell me how to access data from access or sql database and show in the vb form not just as table rather in the form of a windows "form".
---
-- modified at 11:10 Thursday 27th September, 2007
|
|
|
|
|
Please don't post you'r question twice, it will only result in being ignored.
if you want to modify you'r original post you can use the modify link on the right lower corner of the post
I suggest for learning vb.net that you take a simple project and try to complete it, look at the articles here they'v helped me a great deal in accomplishing easy and difficult tasks.
if you run into any trouble you can always ask here but ask specific questions on specific problems.
As to the question where the dataform wizard went, I'm afraid I can't help you with that since I never programmed in 2002.
If you want to display and edit data the datagrid maybe an option (can be complex)
If my help was helpfull let me know, if not let me know why.
The only way we learn is by making mistaks.
|
|
|
|
|
have you tried Google? There are thousands of examples. I suggest you buy a book if you are asking such a basic question.
__________________________
Don't drink and derive.
Alcohol and calculus don't mix.
|
|
|
|
|
hi to all.
i am beginner in vb.net. i was reading a tutorial to make address book in vb.net but that tutorial was using vs.net 2002 in which dataform wizard is available. I am using vs.net 2005 and unable to find such sort of wizard. Please guide me how i can show data from data base to seperate fields in form using wizard or through any graphical process instead of coding.
thx
|
|
|
|
|
You can get an introduction by watching the "Forms over Data Video Series" here[^].
|
|
|
|
|
can i catch any windows focus in vb ?suppose i am running any vb application & i have clicked any windows apps then can i check in vb that focus is on any other window?my os is win 2000.
rahul
|
|
|
|
|
You'd have to implement the "CBT Hook" to get this information. There is no event or class in the .NET Framework that will do this for you. All you have to do is Google for "VB.NET CBT Hook" to find some examples. Warning! There is very little documentation specific to VB concerning hooks. You'll have to translate code from either C++ or C# to get it working.
|
|
|
|
|