• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 651
  • Last Modified:

How do I create a custom input form to insert data into a database with Visual Web Developer? (ASP.NET)

I want to create a custom form layout to insert data into a database using ASP.NET. I can insert data using the DetailsView tool, but I want the data to be laid out in a custom way (not a table) and I don't really want my users to have to click "Insert" to have the fields appear (which DataView requires). Can I do this by editing a template in one of the views or input box by input box? What is the best way of going about this?
0
dswett2
Asked:
dswett2
  • 3
  • 3
  • 2
1 Solution
 
QPRCommented:
input box by input box
then an asp:button

Double click the button (in design view) and do something like....

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Page.IsValid Then
            Dim connectionstring As String = System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
            Dim ObjConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionstring)
            Dim objCmd As SqlCommand = New SqlCommand()
            objCmd.Connection = ObjConn
            Dim name As String = txtName.text
            Dim address As String = txtAddress.Text
            objCmd.CommandType = System.Data.CommandType.StoredProcedure
            objCmd.CommandText = "dbo.InsertNewPerson"
            objCmd.Parameters.Add("@name", Data.SqlDbType.VarChar).Value = name
            objCmd.Parameters.Add("@address", Data.SqlDbType.VarChar).Value = address
            ObjConn.Open()
            objCmd.ExecuteNonQuery()
             ObjConn.Close()
    End Sub
0
 
SethiCommented:
Just inquisitve about the use of  Page.IsValid. Any reasons?
0
 
QPRCommented:
Pasted (and amended) from something I was doing at the time and forgot to exclude.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SethiCommented:
Ok. I thought it had some relevance. Learnig process should be on - that's why I asked :-) Thanks.
0
 
dswett2Author Commented:
QPR, Is there an import statement that needs to go at the beginning? I get Type 'SqlCommand' is not defined.

Also, where is the INSERT SQL statement in this solution?
0
 
QPRCommented:
imports system.data.sqlclient

InsertNewPerson is the name of a stored procedure that excepts 2 (varchar) parameters name and address.
You could alter the code to use a select statement instead of a SP
0
 
dswett2Author Commented:
When I run it, I get a

Could not find stored procedure 'dbo.InsertNewPerson'.

What would the code be if I just wanted to use sql statements? What are the advantages of SPs?
0
 
dswett2Author Commented:
n/m, I got it.

Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now