|
Hi got this code that is a potential solution to my Database login problem.It was originally in VB but I ran a converter on it. Now I have a OleDbDataReader method called Item(String) that was there when the code was in VB but now does not exist on the OleDbDataReader class in C#(dont know if that possible). Here is the full code. I need help to find the equivalent of this Item method in C#. The code connects to a access database and compares a textbox password entry to one in the database under the same user name given on another textbox...Also if there are any changes that I need to make to connect to an SQL Database Server.
private void btnLogin_Click(object sender, System.EventArgs e)
{
//The connection string is used to describe the type of database, the security information and the location to the database.
string ConString = "Provider=Microsoft.Jet.OLEDB.4.0assword=\"\";User ID=Admin;Data Source=\"databasename.mdb\";";
//Create a new connection object and assign the ConString Connection String above
OleDbConnection DBCon = new OleDbConnection(ConString);
// g_login is a global variable defined in a Module that captures the login name and passes it from form to form. To create this, just create a Module, say Module1.vb and in it put "Public g_login as String" {g meaning global and login to represent the global login}
GlobalVariables.g_login = this.textBox1.Text;
string strPassword = this.textBox2.Text;
if (GlobalVariables.g_login == "" || strPassword == "")
{
MessageBox.Show("You are missing information. Please make sure that both the username and password fields are filled out.", "Missing Info");
this.textBox1.Focus();
return;
}
// The database has two fields in the Users table. A UserID field, which is the primary key and declared as a text. The other field is Password, which is a text as well.
string strsql = "SELECT [UserID], [Password] FROM Users WHERE [UserID]='" + GlobalVariables.g_login + "' ";
OleDbCommand cm = new OleDbCommand(strsql, DBCon);
OleDbDataReader dr;
bool valid = false;
bool HasRows = false;
try {
DBCon.Open();
dr = cm.ExecuteReader();
if (dr.HasRows) {
while (dr.Read()) {
if (strPassword == dr.Item("Password")) {
valid = true;
}
}
HasRows = true;
}
dr.Close();
}
catch (OleDbException exO) {
MessageBox.Show(exO.Message);
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
finally {
if (DBCon.State == ConnectionState.Open) {
DBCon.Close();
}
cm = null;
dr = null;
DBCon.Dispose();
GC.Collect();
}
iCount = iCount + 1;
if (valid == true) {
Form n = new Form4();
n.Show();
this.Hide();
}
else if (iCount == 3) {
MessageBox.Show("Contact Developers!", "Invalid Info");
this.Close();
}
else if (HasRows == false) {
MessageBox.Show("Invalid user name, try again!", "Invalid Info");
this.textBox1.Focus();
this.textBox1.Text = "";
this.textBox2.Text = "";
}
else {
MessageBox.Show("Invalid password, try again!", "Invalid Info");
this.textBox2.Focus();
this.textBox2.Text = "";
}
}
Thanks in Advance.
Is this chair taken
|
|
|
|
|
Shouldn't you use square brackets instead of round brackets?
dr.Item("Password") => dr.Item["Password"]
If you used this converter[^], I think that you experienced this:
Known Issues
The converter cannot currently determine whether SomeName(SomeParam) is a method call, or a reference to an indexer - this will not be possible without actually compiling the code or performing some much deeper analysis of the code.
|
|
|
|
|
Dear Developer ,
Have a good day first
I have a little problem in understanding this situation
I created table in SQL 2000
contains two Column 1 - FileName varchar (50)
2 - Size bigint (8)
and I am using C# ADO.NET to insert values in this table with Parameters
and every thing goes well ......
My Question Now , Why the Column Name Must Be "Size" to accept insert Values as BigInt ( in other word when I change the column Name "Size" to any "fooo"
it does not work ) I got message cann't convert nvarchar to bigint
thanx all for your help
P.S
try to see Master.Sysfiles Design Table there is a column named size
I know nothing , I know nothing
|
|
|
|
|
I don't think there is a problem like that...
Could you please show us the code that you are trying to insert the values and the type and name of the table from database?
Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)
If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you.
|
|
|
|
|
I've got a table like this:
title | value | keyId
-----------------------------
aaa | 10 | 1
aaa | 11 | 2
bbb | 10 | 1
ccc | 11 | 2
How can I retrieve "title"s which meet this condition: (keyId=1 and value=10) and (keyId=2 and value=11)
The desired output for above query should be 'aaa'.
Please notice that the following query:
SELECT * FROM table WHERE (keyId=1 AND value=10) AND (keyId=2 AND value=11)
returns nothing, and the following query:
SELECT * FROM table WHERE (keyId=1 AND value=10) OR (keyId=2 AND value=11)
returns all rows while only the first two records are desired.
Please help me!
|
|
|
|
|
First, remember that your WHERE clause is checked against only one row, not multiple rows. What you are aksing for is a check over multiple rows.
What you are asking for is KeyId=1 AND value=10 on one row AND KeyId=2 AND value=11 on another row where title is the same.
So, perhaps what you need is to join the rows together.
SELECT *
FROM (SELECT * FROM table WHERE keyId=1 AND value=10) AS t1
INNER JOIN (SELECT * FROM table WHERE keyId=2 AND value=11) AS t2 ON t1.title = t2.title
Just a thought - I've not tried this so I don't know if it works, but I think it is going in the right direction.
-- Always write code as if the maintenance programmer were an axe murderer who knows where you live.
Upcoming FREE developer events:
* Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ...
* Reading: SQL Bits
My website
|
|
|
|
|
Thank you very much, it's working
I just wonder if you think this is a heavy query and can be rephrased to be performed faster?
|
|
|
|
|
Maysam Mahfouzi wrote: I just wonder if you think this is a heavy query and can be rephrased to be performed faster?
Not really. It isn't that complex. If the table contains a lot of data then you might want to index some of the columns if you need it to perform very fast. (I'd probably go with creating one index containing the columns in the WHERE clause, "KeyId" and "value")
However, don't just blindly create indexes. You must understand how they work and how they affect your database. INSERT s, UPDATE s and DELETE s will be slower with indexes. This is because on each of these commands the index needs to be changed as well as the table. So, if you do a lot of data changes then indexes may slow the overall performance of your database.
If your table consists of just the three columns you showed earlier then there probably isn't much advantage to an index. One of the reasons indexes are faster is that they pull a small amount of data from an index then look up the full row when needed. If the index is similar in size to the full row there isn't anything to be gained. And sometimes it may actually hurt performance.
-- Always write code as if the maintenance programmer were an axe murderer who knows where you live.
Upcoming FREE developer events:
* Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ...
* Reading: SQL Bits
My website
|
|
|
|
|
Hi Colin
Michael Potter has just posted a solution which uses GROUP BY instead of a JOIN. Would you possibly take a look at it and tell us your valuable opinion about it? I guess it should do better in terms of performance due to not using a JOIN.
|
|
|
|
|
I think you can also handle this with an aggregate:
SELECT
title
FROM
table
WHERE
(keyId=1 AND value=10) OR (keyId=2 AND value=11)
GROUP BY title
HAVING COUNT(title) = 2
|
|
|
|
|
Thank you very much for your reply. You've used GROUP BY in a way I could never imagine!
Let's see what Colin Angus Mackay's opinion is about.
|
|
|
|
|
There is often more than one way to achieve the same result. The way I see it is that go with the one that is easier to understand unless it is causing a performance problem. Remember, in the future the requirements might change and someone is going to have to maintain it.
I don't know if that was the answer you were looking for - I was totally non-commital on which solution you should use.
|
|
|
|
|
Colin Angus Mackay wrote: go with the one that is easier to understand unless it is causing a performance problem
I think the same here!
Colin Angus Mackay wrote: I don't know if that was the answer you were looking for
Kind of! It's obvious that using GROUP BY is an easier-to-understand solution, but I just wanted you to say whether it is any better in performance than using a JOIN. You may say that is not necessarily better because GROUP BY is actually using JOINs internally.
|
|
|
|
|
Hi there .
I have cursor that navigate on sysusers table , and get the name of user for drop it ,via exec sp_dropuser @name . but when i want to execute this function give me a error .
Error : Select statements included within a function can not return data to a client
I don't know how can i solve this problem .
My Function :
<br />
CREATE FUNCTION fDropAllSystemUsers()<br />
<br />
returns int<br />
as<br />
begin<br />
declare @result int<br />
declare @name char(20)<br />
<br />
<br />
declare UserNavigator cursor for <br />
select [name] from sysusers where [name] <> 'dbo' and islogin = 1<br />
<br />
open UserNavigator <br />
<br />
if (@@FETCH_STATUS <> 0)<br />
return 0<br />
<br />
while(@@FETCH_STATUS = 0) <br />
begin<br />
exec sp_dropuser @name <br />
fetch next from UserNavigator into @name <br />
end<br />
fetch next from UserNavigator<br />
<br />
close UserNavigator<br />
deallocate UserNavigator<br />
<br />
set @result = 0;<br />
return @result<br />
end <br />
Please help me .
DMASTER
|
|
|
|
|
you are checking for the @@FETCH_STATUS without fetching the record from the cursor...
I suppose the return statement in the middle and the fetch statement at the last are not neccessary...
check with this...
CREATE FUNCTION fDropAllSystemUsers()<br />
<br />
returns int<br />
as<br />
begin<br />
declare @result int<br />
declare @name char(20)<br />
<br />
declare UserNavigator cursor for <br />
select [name] from sysusers where [name] <> 'dbo' and islogin = 1<br />
<br />
open UserNavigator <br />
fetch next from UserNavigator into @name <br />
<br />
while(@@FETCH_STATUS = 0) <br />
begin<br />
exec sp_dropuser @name <br />
fetch next from UserNavigator into @name <br />
end<br />
<br />
close UserNavigator<br />
deallocate UserNavigator<br />
<br />
set @result = 0<br />
return @result<br />
end
Regards John
|
|
|
|
|
I have no clue why you want to delete all the user account and I don't think that you can delete the Builtin Administrator account...
Regards John
|
|
|
|
|
hi everyone,
i want to connect my application to MYSQL database which is located on web.Can anyone provide some code or link which guide me to connect to MYSQL database?
one more thing whether it is essential to install MYSQL server on my system ?
Rupesh Kumar Swami
Software Engineer,
Integrated Solution,
Bikaner (India)
My Company
|
|
|
|
|
Hi there
I want to drop 'builtin\administrators' from sql server with a SQL query or stored procedure.
I logged in with sa user and i don't know how to drop administrators account.
In fact i want to remove windows authentication security login.
How can i do that?
Thanx.
|
|
|
|
|
I'm pretty sure the authentication modes for SQL 2005 are 'Windows Authentication only' and 'Mixed mode' (both). There should only be a couple of built-in accounts - can you just remove their permissions in the management console?
|
|
|
|
|
Yes .
I can remove the login 'BUILTIN\Administrator' or other built-in accounts with management console but i can't remove it with SQL query .
My problem is remove with sql query .
How can I do this ?
DMASTER
|
|
|
|
|
sp_revokelogin 'BUILTIN\Administrator'
|
|
|
|
|
hi all! im not sure but i guess i should have posted my question in this forum since it concerns sql...
my question is regarding querying the database which results in multiple records which should be mapped to struct. can someone please show me how to do this?
i have written vc++ code which queries the database but have only done this with 1 result which contains a few number of fiels which i have assigned to individual variables
now what i would like is to create an array of structs based on multiple records which was selected based on the query.
thanks for any help!
newbie
|
|
|
|
|
You do this in C++ code. Create a vector of structs, and for each row, read the values, build an instance of the struct, and push it into the vector.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I have this stored procedure on which I would like to apply if statements at the where clause. But it seems that sql is finding errors associated with the if statements. Can anyone please guide me on what is wrong. Maybe it is not possible to use if statemnts in where clauses?
@category int,@brand int ---> parameters
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select equipment_id,equipment_modelNumber,equipment_description,category,brand,link,equipment_small_image,equipment_largr_image
FROM Gen_Equipment
INNER JOIN Gen_Equipment_Categories
INNER JOIN Gen_Equipment_Brand
INNER JOIN Gen_Equipment_Link
ON (Gen_Equipment.equipment_category=Gen_Equipment_Categories.category_id)
ON (Gen_Equipment.equipment_brand=Gen_Equipment_Brand.brand)
ON (Gen_Equipment.equipment_link=Gen_Equipment_Link.link_id)
WHERE
if (NOT(@category=0) AND @brand=0) THEN
BEGIN
Gen_Equipment.equipment_category=@category order by Gen_Equipment.equipment_category
END
ELSE IF (@category=0 AND NOT(@brand=0)) THEN
BEGIN
equipment_brand=@brand
END
END
Thank you very much for your help.
Chris
|
|
|
|
|
ChrisFarrugia wrote: I have this stored procedure on which I would like to apply if statements at the where clause. But it seems that sql is finding errors associated with the if statements. Can anyone please guide me on what is wrong. Maybe it is not possible to use if statemnts in where clauses?
You can't use IF statements in WHERE clauses. IF statements stand alone and do not form part of another statement (in this case the SELECT statement)
Your best bet is to put the IF statement around the SELECT
IF ((@category<>0) AND @brand=0)
BEGIN
SELECT equipment_id, equipment_modelNumber, equipment_description, category, brand,
link, equipment_small_image, equipment_largr_image
FROM Gen_Equipment
INNER JOIN Gen_Equipment_Categories
INNER JOIN Gen_Equipment_Brand
INNER JOIN Gen_Equipment_Link
ON (Gen_Equipment.equipment_category=Gen_Equipment_Categories.category_id)
ON (Gen_Equipment.equipment_brand=Gen_Equipment_Brand.brand)
ON (Gen_Equipment.equipment_link=Gen_Equipment_Link.link_id)
WHERE Gen_Equipment.equipment_category=@category
ORDER BY Gen_Equipment.equipment_category
END
ELSE
BEGIN
SELECT equipment_id, equipment_modelNumber, equipment_description, category, brand,
link, equipment_small_image, equipment_largr_image
FROM Gen_Equipment
INNER JOIN Gen_Equipment_Categories
INNER JOIN Gen_Equipment_Brand
INNER JOIN Gen_Equipment_Link
ON (Gen_Equipment.equipment_category=Gen_Equipment_Categories.category_id)
ON (Gen_Equipment.equipment_brand=Gen_Equipment_Brand.brand)
ON (Gen_Equipment.equipment_link=Gen_Equipment_Link.link_id)
WHERE equipment_brand=@brand
END
-- Always write code as if the maintenance programmer were an axe murderer who knows where you live.
Upcoming FREE developer events:
* Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ...
* Reading: SQL Bits
My website
|
|
|
|