asp.net error with invalidcastexception was unhandled by user code

Joined
Jul 16, 2009
Messages
1
Reaction score
0
i have create a login page that take username, password and roles from my MS SQL database but for some reason it will crash at that following line. with error the message as stated below.

Message = Specified cast is not valid.

if (username == myreader.GetString(0) && pwd == myreader.GetString(1) && myreader.GetInt16(2) == level)


below is the exact code i`m using for my login page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.Net.Sockets;
using System.IO;
using System.Web.Caching;

namespace Dummy2
{
public partial class _Default : System.Web.UI.Page
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
/*Declaring all Variables*/
//create a TcpClient
TcpClient client;

// Get a client stream for reading and writing.
Stream stream;

//Declaring ByteRead function
Int32 ByteRead;

// Declaring Packet Number
int pn = 1;

// Declaring 1st bytes buffer to send the message
byte[] str1 = new byte[65519];

//declare inputLen as int
ushort length;
ushort length1;
ushort length2;
ushort length3;

byte sk1; // Session Key 1
byte sk2; // Session Key 2
byte sk3; // Session Key 3
byte sk4; // Session Key 4

byte ar;

ushort aa; // CRC Value
/*End of Declarations*/

//Method for CRC Calculation
static ushort ComputeCRC16(byte[] pData, int length)
{
ushort crc, aaa, bbb;
int idx;
crc = 0;
int index = 0;
while ((length--) > 0)
{
bbb = (ushort)pData[index];
crc ^= (ushort)(bbb << 8);
index++;
for (idx = 0; idx < 8; idx++)
{
aaa = (ushort)(crc & 0x8000);
if ((aaa > 0))
{
crc = (ushort)((crc << 1) ^ (ushort)0x8005);
}
else
{
crc <<= 1;
}
}
}
return (crc);
}
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
}

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login1.UserName;
string pwd = Login1.Password;

string strConn;
strConn = "Server=LoNe4eVa-PC;Database=Logins;uid=sa;pwd=overkill;";
SqlConnection Conn = new SqlConnection(strConn);

//string sqlUserName;
//sqlUserName = "SELECT UserName, Password, Roles FROM Account WHERE (UserName Collate Latin1_General_CI_AI LIKE'" + username + "') AND (Password = '" + pwd + "')";
SqlCommand com = new SqlCommand("SELECT UserName, Password, Roles FROM Account"/*sqlUserName*/, Conn);
Conn.Open();
SqlDataReader myreader = com.ExecuteReader();

bool valid = false;
bool admin = false;

Int16 level = 1;
Int16 level2 = 2;
//string CurrentName;
//CurrentName = (string)com.ExecuteScalar();
do
{

while (myreader.Read())
{
if (username == myreader.GetString(0) && pwd == myreader.GetString(1) && myreader.GetInt16(2) == level)
{
valid = true;
admin = true;
}
else
{
if (username == myreader.GetString(0) && pwd == myreader.GetString(1) && myreader.GetInt16(2) == level2)
{
valid = true;
admin = false;
}

}
}
}
while (myreader.NextResult());

if (valid == true && admin == true)
{
Response.Redirect("Default2.aspx");
}
else
{
if (valid == true && admin == false)
{
try
{
Session["UserAuthentication"] = "";
Session.Timeout = 1;

client = new TcpClient();

client.Connect("192.168.1.254", 4050);


// Get a client stream for reading and writing.
stream = client.GetStream();

//to find crc only
str1[0] = 0x02; //TMID
str1[1] = 0x00; //SK
str1[2] = 0x00; //SK
str1[3] = 0x00; //SK
str1[4] = 0x00; //SK
str1[5] = (byte)(pn & 0xff); //PN
str1[6] = (byte)(pn >> 8); //PN
str1[7] = 0x10; //LEN
str1[8] = 0x00;//LEN
str1[9] = 0x10; //CMD. Values varies according to cmd. Refer to 1.4
str1[10] = 0x00; //CP

//create a checksum
//CRC16 checksum = new CRC16();
int dataLength = 11;
aa = ComputeCRC16(str1, dataLength);

// Declaring 2nd bytes buffer to send the message with CRC
byte[] str2 = new byte[65519];

str2[0] = 0x12; //HC
str2[1] = 0x01; //PV
str2[2] = 0x02; //TMID
str2[3] = 0x00; //SK
str2[4] = 0x00; //SK
str2[5] = 0x00; //SK
str2[6] = 0x00; //SK
str2[7] = (byte)(pn & 0xff); //PN
str2[8] = (byte)(pn >> 8); //PN
str2[9] = 0x10; //LEN
str2[10] = 0x00; //LEN
str2[11] = 0x10; //CMD. Values varies according to cmd. Refer to 1.4
str2[12] = 0x00; //CP
str2[13] = (byte)(aa & 0xff); //CRC
str2[14] = (byte)(aa >> 8); //CRC
str2[15] = 0x92; //EC

pn++; //Adding 1 to the Packet Number

//Send the message to the connected TcpServer.
stream.Write(str2, 0, 16); //stream value from str1a, starting from 0, total 16 byte (str1a.length)

// Get the response
// Declaring bytes buffer to store the response
byte[] str3 = new Byte[16];

// Display the response
ByteRead = stream.Read(str3, 0, str3.Length);

length = str3[9];
sk1 = (byte)length;

length1 = str3[10];
sk2 = (byte)length1;

length2 = str3[11];
sk3 = (byte)length2;

length3 = str3[12];
sk4 = (byte)length3;

ushort length4 = str3[8];
ar = (byte)length4;

Response.Redirect("Default.aspx?Parameter=Session Key 1: " + sk1 +
" Session Key 2: " + sk2 +
" Session Key 3: " + sk3 +
" Session Key 4: " + sk4);


}
catch (SocketException se)
{
Label1.Text = ("There was an error talking to the server" + se.ToString());
Label1.Visible = true;
}
catch (ArgumentNullException f)
{
Label1.Text = ("ArgumentNullException: {0}" + f);
Label1.Visible = true;
}
}
}

}
}
}
 
Top