Comparing Dataset values with Strings

G

Guest

I am creating a login screen wherein only certain users are granted
permission. The names of those users,are copied into a dataset using
dataadapter.Then the dataview is used.

But the problem here is that the "Dv.Count" always evaluates ..to 0. Hence
whenever I enter the valid names "Invalid User" is prompted on the screen

I have writen the code below.

OleDbDataAdapter dAdap1 = new OleDbDataAdapter("SELECT ..[EMPLOYEE ID] FROM
EMPLOYEE;",con1);

dAdap1.Fill(DtSet);

Dv.Table=DtSet.Tables["EMPLOYEE"];

Dv.RowFilter.CompareTo(txtLogin.Text);

if(Dv.Count==0)

{

txtLogin.Text="Invalid User";

}

Kindly suggest,
 
G

Guest

Hi Kunal,

I believe that your problem lies with setting the rowfilter property of the
dataveiw. You are currently using the CompareTo() method (which is a string
method) when you need to be setting this property. You can set it like:

Dv.RowFilter = "columnname = 'columntextvalue'";

so all you have to do for a comparison is place the value of what you want
to match in the columntextvalue and it will reduce your rows.

If you are looking for this to be secure you should take a look at the
Microsoft Patterns and Practices Security guides at

http://www.microsoft.com/resources/practices/application/security.mspx

to get a good handle on what the latest techniques are.

I hope this helps.
-------------------------------
 
A

Andrew Conrad

You are comparing the txtLogin to the string which represents the
expressions which is used as a rowfilter for the DataView.
You will actually need to set the RowFilter to a expression which does the
comparision you require.

Andrew Conrad
Microsoft Corp
http://blogs.msdn.com/aconrad/
 
J

John Smith

Hi kunal,

I think you want to check for an existence of a user. Try the following (its
in vb with sql classes, but your can figure out how to do it in C# with your
oledb source)

Since you want to check, and only check, you need to read, so you can use a
DataReader.

Dim dr as sqlDataReader
Dim cm as new sqlCommand("SELECT UserName FROM tbUser", cn)

cn.open()
dr = cm.ExecuteReader()

do while dr.Read
if txtUsername.Text = CStr(dr("UserName")) then -- In vb, CStr casts the
value to String
' User found
end if
loop

Dont forget to close your datareader and dispose your connection when your
are done.

Ps. This code was not tested
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top