2

I am a beginner in c#. I try to get the last inserted id from an update query. But i get an error This is my code

String Query1 = "insert into test(userid,score) values ('"+userid+"',0);";
SqlConnection conn1 = new SqlConnection(constring);
SqlCommand cmdDatabase1 = new SqlCommand(Query1, conn1);
SqlDataReader dbreader1;
conn1.Open();
testid = (int)cmdDatabase1.ExecuteScalar();
dbreader1 = cmdDatabase1.ExecuteReader();
while (dbreader1.Read())
{    
}

I get this error.

Object reference not set to an instance of an object. on this line testid = (int)cmdDatabase1.ExecuteScalar();

I have also tried this

String Query1 = "insert into test(userid,score) values ('"+userid+"',0);";
SqlConnection conn1 = new SqlConnection(constring);
SqlCommand cmdDatabase1 = new SqlCommand(Query1, conn1);
SqlDataReader dbreader1;
conn1.Open();
object count = cmdDatabase1.ExecuteScalar();
System.Diagnostics.Trace.WriteLine(count.GetType());
int testid = (int)count;
dbreader1 = cmdDatabase1.ExecuteReader();
while (dbreader1.Read())
{    
}

Same error Object reference not set to an instance of an object.o nthis line System.Diagnostics.Trace.WriteLine(count.GetType());

user224447
  • 23
  • 5

4 Answers4

3

For Getting last inserted value from DB, you should write select query.

Not a part of this question, but while using insert or select should make use of parametrized queries to avoid SQLInjections.

This is a link to learn more bout SQLInjections.

your code should be:

try
{
  con.open();
  cmd=new SqlCommand("select max(userid) from test",con);
  da=new SqlDataAdapter(cmd);
  DataSet ds=new DataSet();
  da.fill(ds);
  int lastInsertedId= int.parse(ds.Tables[0].Rows[0][0].toString());
  con.close();
}
catch(exception ex)
{
  messagebox.show(ex.Message);
}

You can also use different techniques in SQL according to SqlServer you are using such as

@@identity,rownumber over. Ofcourse, this can be according to your need and version of SQLServer you are using.

For RowNumber

For @@identity

Freelancer
  • 9,008
  • 7
  • 42
  • 81
2

Use this code to insert as well as select last inserted id. Your main problem was 'Object reference not set to an instance of object'. Try the below code.

try
{
    String Query1 = "insert into test(userid,score) OUTPUT INSERTED.userid values ('"+userid+"',0);";
    SqlConnection conn1 = new SqlConnection(constring);
    SqlCommand cmdDatabase1 = new SqlCommand(Query1, conn1);
    conn1.open();
    da=new SqlDataAdapter(cmdDatabase1);
    DataSet ds=new DataSet();
    da.fill(ds);
    int Id= int.parse(ds.Tables[0].Rows[0][0].toString());
    con.close();
}
catch(exception ex)
{
  messagebox.show(ex.Message);
}
Veer
  • 1,575
  • 3
  • 16
  • 40
0

What line does the error happen on? I'm guessing here:

while (dbreader1.Read())

why? Because an insert query doesn't result in any rows... but I guess we'd have to check that.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
0

For UPDATE, INSERT, and DELETE statements, try using ExecuteNonQuery()

Praveen
  • 55,303
  • 33
  • 133
  • 164