Hello to all,
I want to auto generate id that should be access from my sql database.

I try this code-

Dim j As Integer = 0
        Dim cmd As New SqlCommand("select staff_id from staff ", c.con)

        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader()
        While (dr.Read())
            txtstaff_id.Text = dr("staff_id").ToString()

            j = Convert.ToInt32("txtstaff_id.text".ToString())

            j = j + 1
            txtstaff_id.Text = j.ToString()
        End While
        dr.Close()

error occurs in

j = Convert.ToInt32("txtstaff_id.text".ToString())

please check, Is this in right format?
please reply soon, i have to submit my project on monday.

Edited 5 Years Ago by Narue: added code tags

try this.

j = Convert.ToInt32(txtstaff_id.text.ToString())

also change

select staff_id from staff

to

select max(staff_id) from staff

Edited 5 Years Ago by debasisdas: n/a

try this.

j = Convert.ToInt32(txtstaff_id.text.ToString())

also change

select staff_id from staff

to

select max(staff_id) from staff

thanks debasisdas,
is there need to take sqldatareader?
i am using this

c.con.Open()
Dim j As Integer = 0
Dim cmd As New SqlCommand("select max(staff_id) from staff ", c.con)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
While (dr.Read())
txtstaff_id.Text = dr("staff_id").ToString()

j = Convert.ToInt32("txtstaff_id.text".ToString())

j = j + 1
txtstaff_id.Text = j.ToString()
End While
dr.Close()
c.con.Close()

txtstaff_id.Text = dr("staff_id").ToString()

this line occurs error
please help me if you can..

I would suggest to use

j = dr.GetInt32(0)
txtstaff_id.Text= j.ToString()

You can not use txtstaff_id.Text = dr("staff_id").ToString() because there is no field called staff_id in your select.

When you ask for the MAX(staff_id ) the returned field name is some thing like MaxOfstaff_id.

You can force the returned name to be (IE) MaxId with select max(staff_id) as MaxId from staff and then use dr("MaxId").

Hope this helps

Should be:
txtstaff_id.Text = dr("staff_id").ToString()

You'll get a "no dr exists in this context" error if you do this. 'dr' is an object, and you are treating it as a method. Square brackets is the proper way to do it.

I would suggest to use

j = dr.GetInt32(0)
txtstaff_id.Text= j.ToString()

You can not use txtstaff_id.Text = dr("staff_id").ToString() because there is no field called staff_id in your select.

When you ask for the MAX(staff_id ) the returned field name is some thing like MaxOfstaff_id.

You can force the returned name to be (IE) MaxId with select max(staff_id) as MaxId from staff and then use dr("MaxId").

Hope this helps

In my database, staff_id is my column name and my table name is staff.
i can't get your code please
again post reply using my table's column name.

In green my suggestions.

c.con.Open()
Dim j As Integer = 0
Dim cmd As New SqlCommand("select max(staff_id) from staff ", c.con)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
If dr.Read() then
    j = dr.GetInt32(0) + 1
    txtstaff_id.Text = j.ToString()
End If
dr.Close()
c.con.Close()

Hope this helps

Edited 5 Years Ago by lolafuertes: n/a

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