954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Retrieve Data from autonumber and display it

hye..
I have a database call student which consists of all students information and also student id..
i have created a form that admin have to insert all the data about student..
then after admin click a save button,
i want to display all the information also with the student id(autonumber)..
i have try,but got an error..:(
this is my coding..
can anyone guide me..

OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=dbStd.mdb;");
OleDbCommand cmd = new OleDbCommand("SELECT StdId FROM Student WHERE StdId ='{0}'");
conn.Open();

OleDbDataReader dr = null;
dr = cmd.ExecuteReader();

while(dr.Read())
{
textBox1.Text = dr.GetString(0);
}


dr.Close();
conn.Close();

shxrainz
Newbie Poster
14 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Have you tried @@IDENTITY?

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

i still dont get it..:(
i have tried but many errors occurs

shxrainz
Newbie Poster
14 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

You need to provide some of the errors, and the code you used as we cannot see through our monitors to yours to see whats going on.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

the error data type mismatch

here are my code

string admId = textBox1.Text;
string passw = textBox2.Text;
string strqry = string.Format(@"SELECT AdminId,Password FROM Admin WHERE AdminId ='{0}' AND Password = '{1}'", admId, passw);
string strqry2 = string.Format(@"SELECT @@IDENTITY,StdICNo FROM Student WHERE StdId ='{0}' AND StdICNo = '{1}'", admId, passw);

OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=dbStd.mdb;");
OleDbDataAdapter adapter = null;
try
{
DataTable dt = new DataTable();
conn.Open();
if(rdoBtnAdmin.Checked)
{
adapter = new OleDbDataAdapter(strqry, conn.ConnectionString);
}
else if(rdoBtnStudent.Checked)
{
adapter = new OleDbDataAdapter(strqry2, conn.ConnectionString);

}
adapter.Fill(dt);

if (dt.Rows.Count >0)
{
string username = dt.Rows[0][0].ToString();
string pw = dt.Rows[0][1].ToString();

if (username == admId && pw == passw && rdoBtnAdmin.Checked)
{
//MessageBox.Show("Welcome!");
Admin1 Frm = new Admin1();
Frm.Show();
}
else if(username == admId && pw == passw && rdoBtnStudent.Checked)
{
MessageBox.Show("Welcome Student");
}
else
{
MessageBox.Show("Wrong ID or Password");
}
}
else
{
MessageBox.Show("Wrong ID or Password");
}

}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
finally
{
adapter.Dispose();
//adapter.Close();
conn.Close();
}

shxrainz
Newbie Poster
14 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Kinda hard but, where?

other than not obviously reusing the connection you make to the db only thing I can see would be

MessageBox.Show(exc.ToString());

So, where is the error generated, which line?

only other stab is that it errors on the @@IDENTITY part because you havent added any records to retrieve the @@IDENTITY of the last record added.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

ok let me said it again..
i'm creating a student administration form..

admin will insert all the detail about the student,such as student name,IC and so on and the student id is generate automatically in the database.

so i use this student id as their login id and student IC as their password.

so my problem are,
1) i cannot get the values of the StdId from the database to be compare in the login validation and
2) i also cannot use the StdId values to view all the student information when I using StdId to search student detail.

And for your information i'm new in c# and I also dont know how the @@IDENTITY works and use it.

here the other code for my second problem

OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbStd.mdb");
OleDbCommand com = new OleDbCommand("select * from Student Where StdId = ?", con);
com.Parameters.Add("@StdFirstName", OleDbType.VarChar).Value = textBox1.Text;
con.Open();
com.ExecuteNonQuery();
OleDbDataReader data= com.ExecuteReader();
while (data.Read())
{

textBox9.Text=data["StdFirstName"].ToString();
textBox2.Text=data["StdLastName"].ToString();
textBox3.Text=data["StdICNo"].ToString();
textBox4.Text=data["StdGender"].ToString();
textBox5.Text=data["StdClass"].ToString();
textBox6.Text=data["StdAddress"].ToString();
textBox7.Text=data["StdPhone"].ToString();
textBox8.Text=data["StdEmail"].ToString();

textBox9.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
textBox7.ReadOnly = true;
textBox8.ReadOnly = true;
}
con.Close();

shxrainz
Newbie Poster
14 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

OK, no point getting ranty, firstly you have google, just like the rest of us do when we want to look something up, given you know the name of it, reading more about it should be possible and a lot of the reading will have examples.

OK, you dont say why you cant get stdid from the database, why not? what happens? debugging more often than not will show you why something doesnt work.

Once you have stdid, Im guessing the second part will be part resolved too

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

i have search the code but nothing help me..
i have try to change the StdId datatype from autonumber to text and it works!
but the problem is how can i generate the StdId automatically in a sequence without using autonumber

shxrainz
Newbie Poster
14 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Autonumber field must have value.
look at your table

biran
Newbie Poster
7 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 
Autonumber field must have value. look at your table

Thank you for your input. However, this thread is more than two years old.
I am sure that by now the original problem has been solved.
Please pay attention to the big message above the post window that statesThis thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

Agreed. Closing.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You