A Better was of Inserting?

Jay1b

Well-known member
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
The below calls a stored procedure passing the contents of a number of textboxes as parameters, these are then inserted into a database. However it seems like masses of code for what seems quite simple. It does work perfectly, but i just feel there must be a better way of doing things.

Ive looked at using the SQLDataSource control, but i cant seem to get that working without something like a gridview - which doesnt fit into the way the screen is laid out.

Is there a better way for me to do this? If so, could you please give me a few pointers on what these are?

Thanks.


Code:
         Enter information into user_t table

                Dim conn As New SqlConnection("Data Source=RA\SQLExpress;Initial Catalog=CReq;User ID=ASP;Password=password")
                Dim cmCreateUser As New SqlCommand("dbo.CreateUser", conn)

                cmCreateUser.CommandType = Data.CommandType.StoredProcedure
                conn.Open()

                Dim pmUsername As New SqlParameter
                pmUsername.ParameterName = "Username"
                pmUsername.Value = txtUsername.Text
                cmCreateUser.Parameters.Add(pmUsername)

                Dim pmFirstname As New SqlParameter
                pmFirstname.ParameterName = "Firstname"
                pmFirstname.Value = txtFirstName.Text
                cmCreateUser.Parameters.Add(pmFirstname)

                Dim pmSurname As New SqlParameter
                pmSurname.ParameterName = "Surname"
                pmSurname.Value = txtSurname.Text
                cmCreateUser.Parameters.Add(pmSurname)

                Dim pmALimit As New SqlParameter
                pmALimit.ParameterName = "AuthorisationLimit"
                pmALimit.Value = txtAuthorisationLimit.Text
                cmCreateUser.Parameters.Add(pmALimit)

                Dim pmSiteID As New SqlParameter
                pmSiteID.ParameterName = "SiteID"
                pmSiteID.Value = ddownSite.SelectedValue
                cmCreateUser.Parameters.Add(pmSiteID)

                cmCreateUser.ExecuteNonQuery().ToString

                conn.Close()
 
You could simplify your code using one of the overloaded methods
Code:
cmCreateUser.Parameters.Add("@Username", SqlDbType.Char, <length here>).Value = txtUsername.Text
repeat for other parameters
 
Back
Top