ASP.net 3.5 Insert Method in Class for ObjectDataSource

I'm new to OOP and trying create an insert method to insert a customer in a SQL Server table from a Detail View on a Web page using VB.net in a Customer Class.  

My code so far looks like:

   Public Function InsertCustomer(??) As Integer
      Dim strCnn As String = ConfigurationManager.ConnectionStrings("ConnectionString2").ConnectionString
      Dim cnn As New SqlConnection(strCnn)

      Dim strSQL As String
      strSQL = "INSERT INTO Customer(CustomerName) VALUES(@CustomerName) "
      Dim cmd = New SqlCommand(strSQL, cnn)
      cmd.CommandType = "Text"
      cmd.ExecuteNonQuery()

My problem is how to declare the variables for the Method Parameters to be used in the Query and how to set the @Parameters in the code.   If you have some code that would help, I would really appreciate it.  I've been searching the Web all day without any luck.

Thanks for our help
deepPowdahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
The command object has a Parameters collection. Just add a new parameter object to the parameters collection:

cmd.parameters.add(new SQLParameter("@CustomerName", <variable type>, <max length>, <value>)

See this MSDN article:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx
0
deepPowdahAuthor Commented:
Thanks for your response.  I understand how to add parameters.  My problem is how to get the value of the parameter into the method.  Does the DetailsView provide an Object that I can use?  If not how do I set the value of the parameter?

Thanks
0
Shaun KlineLead Software EngineerCommented:
Yeah, I guess my answer really doesn't answer that question.

Look at the DetailsView.ItemUpdating Event. The EventArgs for that event is DetailsViewUpdateEventArgs, which has properties of Keys, OldValues and NewValues.

From there, you can get the new values of your fields and pass them to your method. In the example you provided, you would pass new value for CustomerName as a string:

Public Function InsertCustomer(ByVal CustomerName as String) As Integer
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

deepPowdahAuthor Commented:
I'm still a little confused (acutually a lot).  When I use a SQLDataSource with a form View, the form view source is automatically generated with all the fields in all the templates.  However, when I use an ObjectDataSource with a DetailsView, the data is displayed but the source does not include the fields.  Is this correct?  Here is my source.

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        SelectMethod="GetCustomer" TypeName="Customer"
        InsertMethod="InsertCustomer">
        <InsertParameters>
            <asp:Parameter Name="Customer" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>

<asp:DetailsView ID="DetailsView1" runat="server"
        DataSourceID="ObjectDataSource1" Height="50px" Width="125px"
        AllowPaging="True" AutoGenerateRows="False">
        <Fields>
            <asp:CommandField ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>

In my Customer class, I have the code for the InsertCustomer shown in my first post.  

I have many other fields in the Customer table, but I wanted to test it by inserting only the CustomerName.  I get the following exception.

InvalidOperationException: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'InsertCustomer' that has parameters: CustomerName, CustomerID, AddName, AddDate, ModName, ModDate, DelName, DelDate, RecStat.]

My query only includes CustomerName.  I don't know if it recognizes CustomerName or it needs all fields in the record.

I'm hoping I have expressed my issues.

 
0
Shaun KlineLead Software EngineerCommented:
To use the objectdatasource, your insert function needs to be Public Shared.
0
deepPowdahAuthor Commented:
Thanks I had already corrected the code but it did not appear in my question.  
0
Shaun KlineLead Software EngineerCommented:
Try changing
        <InsertParameters>
            <asp:Parameter Name="Customer" Type="String" />
        </InsertParameters>
to
        <InsertParameters>
            <asp:Parameter Name="CustomerName" Type="String" />
        </InsertParameters>

If that doesn't work, try including all of the parameters that are returned from your Select method.
Also, this post has different solutions that other people have tried: http://geekswithblogs.net/chrishan/archive/2005/07/13/46223.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.