how to disply last enter data of a particular name

like
table name = abc

contents in the table :
1.abc
2.pqr
3.abc
4.xyz

now in visual studio i want to search last enter data of abc

I have created table in sql 2005 for back end and in front end i use c# from Form data save in the SQL table. now i have done some coding to search the data from table but it search only first entered data. like as above if i have enter two records with the same name but diffrent primary key no. it always show the first enterd data with that particular name.

i hope you understand the problem

here is the code:

SqlConnection cn = new SqlConnection(s);
cn.open();
SqlCommand cmd = new SqlCommand();
SqlDataReader rdr;
string cd = combobox1.Text.Tostring();
string CommandText = "select * from abc where code=@code";
cmd = new SlqCommand(CommandText);
cmd.Connection = cn;
cmd.Parameters.Add (newSqlParameter("@code",System.SData.SqlDbType.VarChar,20,"code"));
cmd.Parameters["@code"].Value = ComboBox1.Text.Tostring();
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
textBox1.Text = rdr["code"].ToString();
tetBox2.Text = rdr["name"].ToString();
}

This code search the data. but shows only first ented data only.

I have use databindingfor that but it give me error message i.e.

Object referance is not set to an object referance.

Binding bnd = textBox1.DataBindings["Text"];
bnd.BindingManagerBase.Position = bnd.BindingManagerBase.Count - 1;

Edited 5 Years Ago by virusisfound: n/a

This code will give you the last record

SELECT * FROM  Table_name WHERE  ID=( SELECT MAX(ID) FROM Table_name)

but for most recent entry you should add a column in your table which holds the time of insertion of record

u dont understand

THis line will search tables last entered data. I want to search particular names last save data.


if i enterd three record in table with name abc then i want to search third data.

i use this line of code. this will give me error message i.e. indexoutofrange can you tell me why this error occured. i tried so hard to solve this problem but its still not slved please tell me why this error occured.

SqlConnection cn = new SqlConnection(s);
cn.open();
SqlCommand cmd = new SqlCommand();
SqlDataReader rdr;
string cd = combobox1.Text.Tostring();
//insted of this line [B]string CommandText = "select * from abc where code=@code";[/B]
i use this line
[B]SELECT * FROM  abc WHERE  ID=( SELECT MAX(ID) FROM abc);[/B]
cmd = new SlqCommand(CommandText);
cmd.Connection = cn;
cmd.Parameters.Add (newSqlParameter("@code",System.SData.SqlDbType.VarChar,20,"code"));
cmd.Parameters["@code"].Value = ComboBox1.Text.Tostring();
rdr = cmd.ExecuteReader();
while(rdr.Read())
{

textBox1.Text = rdr["code"].ToString();
tetBox2.Text = rdr["name"].ToString();
}

Edited 5 Years Ago by virusisfound: n/a

The error might be on any other line as on while statement because the command you used is right debug your code by adding break points and trace where exception is rising because normally this exception rises when we exceed the length of a control

Edited 5 Years Ago by abelLazm: n/a

thank you for reply me the code is working. the problem was id the id number was not save properly thats way the program runs but when i tried to search data it gve me error. thank you so much for replay and solve my problem. thank you once again.....

my problem is sloved but i need a small help if you can please

can you tell me a queary for search record which has same name same fields?
how to search one record from them? or how to search last enterd record on name field if the duplication is allowed?

Thank You.

Edited 5 Years Ago by virusisfound: n/a

You can access it if you know the row number other wise both the records will be retrieved why are you not removing the duplicate entries?

because i am saving the data with name field. its necessary to save data with name it has unique primary key. But searching is perform by the name. and i want to search all data enter with same name from table and also the last save data of that particular name also. I have the code to search the entire data but only i cant search the last save data.

I had the same problem and i tried to look for the solution i found out the only way to do this to keep the log from start mean add a new column in the table which will hold the time of insertion of a record and you will be able to get the last saved record other than keeping log there is no way as far as |I know

i got a solution on that in disconnected envitnment with fill by queary
here is the code:

private void fillBy1ToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.saledtlTableAdapter.FillBy1(this.sportDataSet.saledtl, itnumToolStripTextBox1.Text);
[B]                Binding bnd = textBox4.DataBindings["Text"];
                bnd.BindingManagerBase.Position = bnd.BindingManagerBase.Count - 1;[/B]            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

Binding bnd = textBox4.DataBindings["Text"];
bnd.BindingManagerBase.Position = bnd.BindingManagerBase.Count - 1;

these two lines search the last enter data. but the problem is when i try to do this in connected envirnment it show me error message i.e.

object referance is not set to an instanv\ce of an object.

if you can do with this code please tell me.

This article has been dead for over six months. Start a new discussion instead.