Reputation: 179
I am returning the retrieved value into a variable but after checking whether the rows exist or not. But the if condition fails as there are currently no records in the table.
Dim sqlstr as string
Dim da As SqlClient.SqlDataAdapter
sqlstr = "select max(mat_req_no) as mat_req_no from pos_mrq_hdr"
If dt.Rows.Count > 0 Then
ltino = dt.Rows(0)("mat_req_no").tostring
End If
the if dt.rows.count > 0
Upvotes: 1
Views: 1516
Reputation: 633
you may forget some statements in your code , But when I test in Max
it will return always (1) in row count so we should test returned value if NULL or NOT
the complete code will be
Imports System.IO
Imports System.Data.SqlClient
Public Class Form1
Dim cnn As SqlConnection
Dim connectionString As String
Dim sqlAdp As SqlDataAdapter
Dim ds As New DataSet
Dim dt As New DataSet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=userid; Password=password"
cnn = New SqlConnection(connectionString)
cnn.Open()
sqlAdp = New SqlDataAdapter("select max(mat_req_no) as mat_req_no from pos_mrq_hdr", cnn)
cnn.Close() 'connection close here , that is disconnected from data source
sqlAdp.Fill(ds)
sqlAdp.Fill(dt)
'fetching data from dataset in disconnected mode
' MsgBox(ds.Tables(0).Rows.Count)
If IsDBNull(ds.Tables(0).Rows(0).Item(0)) Then
' MsgBox("no")
Else
Dim ltino = ds.Tables(0).Rows(0)("mat_req_no").ToString
' MsgBox(ltino)
End If
End Sub
End Class
Upvotes: 1