checking if the dataset is empty?

S

sean

HI There,

How can I check if the dataset is empty? I just want to check to see if the
dataset is empty or null when the query is run, couls someone help me out
with some code please?

Sean


Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim DS As New DataSet()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(DS, "tblListings")

Return DS.Tables("tblListings").Copy
End Function

Sub doPaging(ByRef CategoryID)
pagedData.DataSource = getTheData(CategoryID).DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10

Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString()
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

btnPrev.Visible = ( NOT pagedData.IsFirstPage )
btnNext.Visible = ( NOT pagedData.IsLastPage )

pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " &
pagedData.PageCount

theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub
 
M

Miha Markic [MVP C#]

Hi sean,

sean said:
HI There,

How can I check if the dataset is empty? I just want to check to see if the
dataset is empty or null when the query is run, couls someone help me out
with some code please?

You should check the table and not the dataset.
getTheData(CategoryID).Rows.Count == 0 means that there are no rows within
the table.
Btw, why are you creating a dataset and returning a table copy?

The function below should work better :)

Function getTheData(ByVal CategoryID as Integer) As DataTable
 
S

sean

Hi There,

Thanks for helping me out, in relation to your question "why are you
creating a dataset and returning a table copy", I tried just returning the
dataset on it's own and I got an error saying the object reference was not
set? Is there a better way of doing it?

Sean


Miha Markic said:
Hi sean,

sean said:
HI There,

How can I check if the dataset is empty? I just want to check to see if the
dataset is empty or null when the query is run, couls someone help me out
with some code please?

You should check the table and not the dataset.
getTheData(CategoryID).Rows.Count == 0 means that there are no rows within
the table.
Btw, why are you creating a dataset and returning a table copy?

The function below should work better :)

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim Dt As New DataTable()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(Dt)

Return DT
End Function

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim DS As New DataSet()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(DS, "tblListings")

Return DS.Tables("tblListings").Copy
End Function

Sub doPaging(ByRef CategoryID)
pagedData.DataSource = getTheData(CategoryID).DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10

Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString()
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

btnPrev.Visible = ( NOT pagedData.IsFirstPage )
btnNext.Visible = ( NOT pagedData.IsLastPage )

pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " &
pagedData.PageCount

theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub
 
M

Miha Markic [MVP C#]

Can you show us some code?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

sean said:
Hi There,

Thanks for helping me out, in relation to your question "why are you
creating a dataset and returning a table copy", I tried just returning the
dataset on it's own and I got an error saying the object reference was not
set? Is there a better way of doing it?

Sean


Miha Markic said:
Hi sean,

sean said:
HI There,

How can I check if the dataset is empty? I just want to check to see
if
the
dataset is empty or null when the query is run, couls someone help me out
with some code please?

You should check the table and not the dataset.
getTheData(CategoryID).Rows.Count == 0 means that there are no rows within
the table.
Btw, why are you creating a dataset and returning a table copy?

The function below should work better :)

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim Dt As New DataTable()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(Dt)

Return DT
End Function

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim DS As New DataSet()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(DS, "tblListings")

Return DS.Tables("tblListings").Copy
End Function

Sub doPaging(ByRef CategoryID)
pagedData.DataSource = getTheData(CategoryID).DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10

Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString()
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

btnPrev.Visible = ( NOT pagedData.IsFirstPage )
btnNext.Visible = ( NOT pagedData.IsLastPage )

pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " &
pagedData.PageCount

theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub
 
S

sean

of course I didn't write the code, I just adapted it to my needs. Which was
to be able to scroll through a returned set of rows and have the flexability
to return the fields that I needed.

Sean



<script language="vb" runat="server">

Dim pagedData As New pagedDataSource

Sub Page_Load(byVal obj As Object, byVal e As EventArgs)
Dim CategoryID as Integer
If Not IsNumeric(trim(request.querystring("StateID")))
response.redirect("error.aspx")
Else
CategoryID = request.querystring("StateID")
doPaging(CategoryID)
End if
End Sub

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim DS As New DataSet()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN tbLocation
G ON L.location = G.LocationID WHERE L.Location=" & CategoryID, strConnect)
objSQLAdapter.Fill(DS, "tblListings")

Return DS.Tables("tblListings").Copy
End Function

Sub doPaging(ByRef CategoryID)
pagedData.DataSource = getTheData(CategoryID).DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10

Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString()
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

btnPrev.Visible = ( NOT pagedData.IsFirstPage )
btnNext.Visible = ( NOT pagedData.IsLastPage )

pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " &
pagedData.PageCount

theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub

Public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)
Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" &
(pagedData.CurrentPageIndex - 1)& "&StateID=" &
request.querystring("StateID"))
End Sub

Public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)
Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" &
(pagedData.CurrentPageIndex + 1)& "&StateID=" &
request.querystring("StateID"))
End Sub
</script>

<form runat="server">

<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<table width="640" border="0" cellpadding="0" cellspacing="0">
<td colspan="2" valign=top><UserControl2:Header runat="server" /></td>
<tr>
<td width="100" valign=top><UserControl1:Menu runat="server" /></td>
<td width="563" valign=top>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<tr bgcolor="purple">
<td><span style="color: white;">Make & Model</span></td><td><span
style="color: white;">Price</span></td><td><span style="color:
white;">Manufacturer</span></td><td><span style="color:
white;">State</span></td>
</tr>
</table>

<asp:DataList id="theDataList" runat="server" border="1" cellpadding="3"
cellspacing="0" width="500">

<AlternatingItemStyle BackColor="#EFEFEF" />
<ItemTemplate>


<tr>
<td align="right" width="100"><%# DataBinder.Eval(Container.DataItem,
"modeldesc") %></td><td align="right" width="60"><%#
DataBinder.Eval(Container.DataItem, "listprice")%></td><td align="right"
width="100"><%# DataBinder.Eval(Container.DataItem, "manufacturer")
%></td><td align="right" width="140"><%# DataBinder.Eval(Container.DataItem,
"location") %></td>
</tr>


</ItemTemplate>

</asp:DataList>

<asp:LinkButton id="btnPrev" Text="Previous Page" OnClick="Prev_Click"
runat="server" />
<asp:LinkButton id="btnNext" Text="Next Page" OnClick="Next_Click"
runat="server" />
<asp:label id="pageNumber" runat="server" />



Miha Markic said:
Can you show us some code?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

sean said:
Hi There,

Thanks for helping me out, in relation to your question "why are you
creating a dataset and returning a table copy", I tried just returning the
dataset on it's own and I got an error saying the object reference was not
set? Is there a better way of doing it?

Sean


Miha Markic said:
Hi sean,

HI There,

How can I check if the dataset is empty? I just want to check to see if
the
dataset is empty or null when the query is run, couls someone help
me
out
with some code please?

You should check the table and not the dataset.
getTheData(CategoryID).Rows.Count == 0 means that there are no rows within
the table.
Btw, why are you creating a dataset and returning a table copy?

The function below should work better :)

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim Dt As New DataTable()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN
tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(Dt)

Return DT
End Function

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com


Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim DS As New DataSet()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN
tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(DS, "tblListings")

Return DS.Tables("tblListings").Copy
End Function

Sub doPaging(ByRef CategoryID)
pagedData.DataSource = getTheData(CategoryID).DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10

Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString()
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

btnPrev.Visible = ( NOT pagedData.IsFirstPage )
btnNext.Visible = ( NOT pagedData.IsLastPage )

pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " &
pagedData.PageCount

theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub
 
S

sean

I ended up using the following to check for empty

pagedData.Count = 0


sean said:
of course I didn't write the code, I just adapted it to my needs. Which was
to be able to scroll through a returned set of rows and have the flexability
to return the fields that I needed.

Sean



<script language="vb" runat="server">

Dim pagedData As New pagedDataSource

Sub Page_Load(byVal obj As Object, byVal e As EventArgs)
Dim CategoryID as Integer
If Not IsNumeric(trim(request.querystring("StateID")))
response.redirect("error.aspx")
Else
CategoryID = request.querystring("StateID")
doPaging(CategoryID)
End if
End Sub

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim DS As New DataSet()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN tbLocation
G ON L.location = G.LocationID WHERE L.Location=" & CategoryID, strConnect)
objSQLAdapter.Fill(DS, "tblListings")

Return DS.Tables("tblListings").Copy
End Function

Sub doPaging(ByRef CategoryID)
pagedData.DataSource = getTheData(CategoryID).DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10

Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString()
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

btnPrev.Visible = ( NOT pagedData.IsFirstPage )
btnNext.Visible = ( NOT pagedData.IsLastPage )

pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " &
pagedData.PageCount

theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub

Public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)
Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" &
(pagedData.CurrentPageIndex - 1)& "&StateID=" &
request.querystring("StateID"))
End Sub

Public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)
Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" &
(pagedData.CurrentPageIndex + 1)& "&StateID=" &
request.querystring("StateID"))
End Sub
</script>

<form runat="server">

<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<table width="640" border="0" cellpadding="0" cellspacing="0">
<td colspan="2" valign=top><UserControl2:Header runat="server" /></td>
<tr>
<td width="100" valign=top><UserControl1:Menu runat="server" /></td>
<td width="563" valign=top>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<tr bgcolor="purple">
<td><span style="color: white;">Make & Model</span></td><td><span
style="color: white;">Price</span></td><td><span style="color:
white;">Manufacturer</span></td><td><span style="color:
white;">State</span></td>
</tr>
</table>

<asp:DataList id="theDataList" runat="server" border="1" cellpadding="3"
cellspacing="0" width="500">

<AlternatingItemStyle BackColor="#EFEFEF" />
<ItemTemplate>


<tr>
<td align="right" width="100"><%# DataBinder.Eval(Container.DataItem,
"modeldesc") %></td><td align="right" width="60"><%#
DataBinder.Eval(Container.DataItem, "listprice")%></td><td align="right"
width="100"><%# DataBinder.Eval(Container.DataItem, "manufacturer")
%></td><td align="right" width="140"><%# DataBinder.Eval(Container.DataItem,
"location") %></td>
</tr>


</ItemTemplate>

</asp:DataList>

<asp:LinkButton id="btnPrev" Text="Previous Page" OnClick="Prev_Click"
runat="server" />
<asp:LinkButton id="btnNext" Text="Next Page" OnClick="Next_Click"
runat="server" />
<asp:label id="pageNumber" runat="server" />



Miha Markic said:
Can you show us some code?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

sean said:
Hi There,

Thanks for helping me out, in relation to your question "why are you
creating a dataset and returning a table copy", I tried just returning the
dataset on it's own and I got an error saying the object reference was not
set? Is there a better way of doing it?

Sean


"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
Hi sean,

HI There,

How can I check if the dataset is empty? I just want to check to
see
if
the
dataset is empty or null when the query is run, couls someone help me
out
with some code please?

You should check the table and not the dataset.
getTheData(CategoryID).Rows.Count == 0 means that there are no rows within
the table.
Btw, why are you creating a dataset and returning a table copy?

The function below should work better :)

Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim Dt As New DataTable()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN
tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(Dt)

Return DT
End Function

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com


Function getTheData(ByVal CategoryID as Integer) As DataTable
Dim DS As New DataSet()
Dim strConnect As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim objSQLAdapter As New SQLDataAdapter("SELECT L.manufacturer,
L.modeldesc, L.listprice,G.Location FROM tblListings L INNER JOIN
tbLocation
G ON L.location = G.LocationID WHERE formid=" & CategoryID, strConnect)
objSQLAdapter.Fill(DS, "tblListings")

Return DS.Tables("tblListings").Copy
End Function

Sub doPaging(ByRef CategoryID)
pagedData.DataSource = getTheData(CategoryID).DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10

Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString()
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

btnPrev.Visible = ( NOT pagedData.IsFirstPage )
btnNext.Visible = ( NOT pagedData.IsLastPage )

pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " &
pagedData.PageCount

theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Datalist Question 1

Top