Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Sunday, March 11, 2012

another question about bind object to Report

hi,all
I download the Sample from http://www.gotreportviewer.com
in the "InvoiceMaker" sample,
the object like Custome can be bind to the Report directly,
but in the "Object Data Source" Sample ,I Modify the
" reportViewer.LocalReport.DataSources.Add(
new ReportDataSource("Product", merchant.GetProducts()));"
to "reportViewer.LocalReport.DataSources.Add(
new ReportDataSource("Product", new new Product("Pencil", 30)))"
there was an Exception with infomation:"Value does not fall within the expected range "


what's wrong with it?

LocalReport.DataSources takes a collection of objects, it doesn't allow a single element - that's why you get the error.

The reason why the InvoiceMaker works with a single element is because it uses a System.Windows.Forms.BindingSource - which can take either a list, an object or a type.

Here's the "magic" that makes it work in InvoiceMaker

Me.CustomerBindingSource = New System.Windows.Forms.BindingSource(Me.components)

...

Me.CustomerBindingSource.DataSource = GetType(ReportFromGrid.Customer)

...

ReportDataSource1.Value = Me.CustomerBindingSource

...

Me.CustomerBindingSource.DataSource = New Customer()

The ObjectDataSource sample takes a shortcut, and shows you how to use a collection directly, w/o going through the binding. That makes things simpler but removes some of the syntactic sugar that makes it easier to use.

Hope that helps,

Tudor Trufinescu

|||I know,thanks!

Saturday, February 25, 2012

Annoying, cant insert into DB for some reason, even using a stored procedure.

Hello, I am having problems inserting information into my DB.

First is the code for the insert


Sub AddCollector(Sender As Object, E As EventArgs)
Message.InnerHtml = ""

If (Page.IsValid)

Dim ConnectionString As String = "server='(local)'; trusted_connection=true; database='MyCollection'"
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As SqlCommand
Dim InsertCmd As String = "insert into Collectors (CollectorID, Name, EmailAddress, Password, Information) values (@.CollectorID, @.Name, @.Email, @.Password, @.Information)"

myCommand = New SqlCommand(InsertCmd, myConnection)

myCommand.Connection.Open()

myCommand.Parameters.Add(New SqlParameter("@.CollectorID", SqlDbType.NVarChar, 50))
myCommand.Parameters("@.CollectorID").Value = CollectorID.Text

myCommand.Parameters.Add(New SqlParameter("@.Name", SqlDbType.NVarChar, 50))
myCommand.Parameters("@.Name").Value = Name.Text

myCommand.Parameters.Add(New SqlParameter("@.Email", SqlDbType.NVarChar, 50))
myCommand.Parameters("@.Email").Value = EmailAddress.Text

myCommand.Parameters.Add(New SqlParameter("@.Password", SqlDbType.NVarChar, 50))
myCommand.Parameters("@.Password").Value = Password.Text

myCommand.Parameters.Add(New SqlParameter("@.Information", SqlDbType.NVarChar, 3000))
myCommand.Parameters("@.Information").Value = Information.Text

Try
myCommand.ExecuteNonQuery()
Message.InnerHtml = "Record Added<br>"
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "ERROR: A record already exists with the same primary key"
Else
Message.InnerHtml = "ERROR: Could not add record"
End If
Message.Style("color") = "red"
End Try

myCommand.Connection.Close()

End If

End Sub

No matter what I get a "Could not add record" message

Even substituting the insert command string with my stored procedure I would get the same thing

Stored Procedure:


CREATE Procedure CollectorAdd
(
@.Name nvarchar(50),
@.Email nvarchar(50),
@.Password nvarchar(50),
@.Information nvarchar(3000),
@.CustomerID int OUTPUT
)
AS

INSERT Collectors
(
Name,
EMailAddress,
Password,
Information
)

VALUES
(
@.Name,
@.Email,
@.Password,
@.Information
)
GO

Can anyone see any problems with this code? It looks good to me but I get the same message always.

ThanksWhy not print out the actual exception text (exp.ToString(), for instance)? Then the exception will tell you why.|||Wow, nice little trick. It helped me find the problem. I had an expected parameter in the SP that I was not supplying.

Thank You