Unable to detect what is wrong with my code?!

  • Thread starter Thread starter Butt Chin Chuan
  • Start date Start date
B

Butt Chin Chuan

Hello everyone!

I got a serious problem here which I tried for a week
debugging it but cannot solve it. The problem is this: I
tried to post a reply in my application to a previous
post, and the code is the following:

sqlconn.Open()
sql = "SELECT count(Message_ID) as CurrentMaxID from
tblReplyMessage"
Dim objAdapter1 As New SqlCeDataAdapter(sql, sqlconn)
objAdapter1.Fill(objDataset, "MID")
objDataTable1 = objDataset.Tables("MID")
Dim InsertID As Integer = CInt(objDataTable1.Rows(0).Item
("CurrentMaxID")) + 1
sqlconn.Close()
MsgBox("InsertID = ", InsertID)

Now, in my message box, I cant get anything out of it, I
get the message ("InsertID = "), meaning there isnt any
return value in my select. However, I tried in SQL2000 and
SQLCE Query Analyzer, and I got my results. The adapter
and the datatable, although i used it in other forms, is
localized (Private). What is the possible cause here? I
really need my work getting done, and I (hopefully not my
careless mistake somewhere) am getting stuck here like
forever. Please comment, your help is very much
appreciated! :)
 
Thanks, that's silly of me doing such a mistake, now my
program still cannot work, but at least i now know it has
nothing to do with my posted code. Thanks again! Waiting
someone to tell me how to local database in the emulator
though....
 
Just wanted to mention that what you do here can be done much easier:

sqlconn.Open()
Dim cmd as SqlCeCommand = sqlconn.CreateCommand()
cmd.CommandText = "SELECT count(Message_ID) from tblReplyMessage"
Dim InsertID As Integer = CInt(cmd.ExecuteScalar()) + 1
sqlconn.Close()
MsgBox("InsertID = " & InsertID)

Also somehow I think in the query you need Max(Message_ID) instead of
Count(Message_ID)
 
Back
Top