index > .NET Framework Data Access and Storage > DataSet, SqlCommand...

DataSet, SqlCommand...

Hi,

I have function that gets a datatable

Public Function PageGetByID(ByVal id As String) As DataTable

Try

Dim par() As SqlParameter = New SqlParameter(1) {}

par(0) = New SqlParameter("?id", id)

Dim ds As DataSet = dl.getDataset("select * from News where id = ?id", par)

If ds.Tables(0).Rows.Count > 0 Then

Return ds.Tables(0)

End If

Return Nothing

Catch

Return Nothing

End Try

End Function

Later I use it to get string of html from my db and show it through Literal

Dim dt As DataTable = PageGetByID("1")

Literal1.Text = dt.Rows(0)("Page").ToString()

But I have an exeption in getDataset emthod of DataLayer object. Here is this method:

Public Function getDataset(ByVal sql As String, ByVal ParamArr() As SqlParameter) As DataSet

Dim conn As SqlConnection

Dim da As SqlDataAdapter = Nothing

Dim cmd As SqlCommand = Nothing

conn = New SqlConnection(ConStr)

Try

conn.Open()

Dim data As DataSet = New DataSet()

cmd = New SqlCommand(sql, conn)

Dim i As Integer

For i = 0 To (ParamArr.Length - 1) Step i + 1

cmd.Parameters.Add(ParamArr(i))

Next

da = New SqlDataAdapter(cmd)

da.Fill(data) ', "data");

Return data

Catch mex As SqlException

Throw mex

Catch ex As Exception

Throw ex

Finally

If conn.State = ConnectionState.Open Then

conn.Close()

conn.Dispose()

cmd.Dispose()

da.Dispose()

End If

End Try

End Function

In yellow block try catches an null-exeption. What is a problem?

juayen

Hi,

Try using SqlParameterColleciton instead of an array in storing SqlParamters.

http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.aspx

cheers,

Paul June A. Domag




Visit my Online Blog - http://webmag.mvps.org
Paul Domag

To debug this, add a breakpoint on your for loop. At this point, check the values of your array, and its length. Not sure about a foreach loop in vb.net, but in c#, try

foreach(SqlParameter sqlParam in Arrayname)

cmd.Parameters.add(sqlparam);

You may find that you never go into this loop, because the length of your array is 0.




ryan
ryan031581
But what is a way out?
juayen

Do you mean what is equal to EndIf? In C# the first line of code under a loop, or if statement is the only one that executes if conditions are met. If there are multiple lines the sytax is

foreach(whatever)

{

}




ryan
ryan031581
reply 5

You can use google to search for other answers

 

More Articles

• transaction problem
• interacting applications
• Help with queries
• using vs.net2005 dataset
• You help will be appreciated: Connection to DBF free table files
• Updating SQL Server using DataSet
• No value given for one or more required parameters- error - oledb...
• Sproc insert problem
• Update Excel Data
• how to read only certain fields(columns) from a loaded dataset
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Typed DataSet Class not exposing its mem
• Getting deleted child rows for a "S
• suggestion for data manipulation code op
• Visual Studio 2003.net Databases Connect
• Merging datasets causes an exception wit
• Updates Using the Entreprise Library DAAB
• Datasets and SQL Server
• database diagram node in server explorer
• Problem Importing Data From Excel
• Single DateSource - Multiple Databases
• How do I change the location of "|D
• Application Blocks
• DISPLAYING TABLES OF AN ACCESS DATABASE
• Microsoft Oracle .NET Data Provider Perf
• Access to path to the database was denied

Hot Articles

• How to Pass in a SQL Decimal with Precis
• SQL Server 2005 JDBC Driver and x64 sys
• Determine Primary Keys using SqlConnecti
• Datatable value not displaying!!!
• Data type mismatch in criteria expression
• Oracle not listed as a data source
• OnTableNewRow for a derived DataTable do
• Typed DataSet Class not exposing its mem
• {"Object must implement IConvertibl
• Microsoft Oracle .NET Data Provider Perf
• database MDI application Dataset update
• Sample Code
• Table Adapter, set foreign key fields on
• How Do I Load MySQL DB Data
• Data Insertion Updation Deletion in Mult

Recommend Articles

• DataFormatString for DateTime column
• no option to connect system.data.oledb
• ODbc Connection String to Excel
• can we call myDataSet.AcceptChanges befo
• Variables in TableAdapter queries...
• import / parse DIF files (Data Interchan
• DataView with DateTime comparation
• Trying to insert non-latin characters in
• Enterprise Library for .NET Framework 2.
• Errors creating new data source
• ExecuteNonQuery not working within CLR S
• Problems with running an Access macro in
• Stored Procedures takes long time to exe
• Data type mismatch in criteria expression
• Can not detach database right after back