Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using SQL to store values in a database in VB.NET 2005

Posted on 2006-07-16
3
Medium Priority
?
252 Views
Last Modified: 2010-04-23
Hi, I would like to Store data from a form in a SQL database. I would like a method to do this without using databinding.

I have the connection string already

Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")

I would like a way to be able to say

database.additem
Name.text = NAME
etc
database.save

with NAME being a field in the database.

Thank you in advance for all your help!

0
Comment
Question by:OsirisJa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 1200 total points
ID: 17119765
Ok here is a sample how yhou can proceed
I have taken an eg. where you want to store Employee data
so Have a class like this


Public Class Database

    Dim m_EmpName As String
    Dim m_EmpID As String
    Dim m_EmpSalary As Integer

    Public Property EmpName() As String
        Get
            Return m_EmpName
        End Get
        Set(ByVal value As String)
            m_EmpName = value
        End Set
    End Property

    Public Property EmpID() As String
        Get
            Return m_EmpID
        End Get
        Set(ByVal value As String)
            m_EmpID = value
        End Set
    End Property

    Public Property EmpSalary() As Integer
        Get
            Return m_EmpSalary
        End Get
        Set(ByVal value As Integer)
            m_EmpSalary = value
        End Set
    End Property

    Public Sub Save()
        Dim Cn As New SqlConnection(YourConnectionString)
        Dim Qry As String = String.Format("Insert Into Emp (EmpID, EmpName, EmpSalary) values ('{0}','{1}',{2})", EmpID, EmpName, EmpSalary)
        Dim Cmd As New SqlCommand(Qry, Cn)
        Try
            Cn.Open()
            Cmd.ExecuteNonQuery()
        Catch ex As Exception
        Finally
            Cn.Close()
            Cn.Dispose()
            Cmd.Dispose()
        End Try
    End Sub
End Class

The class has 3 properties for the 3 database fields that you want to Update and a method called Save that will save the data to the Db

Now in order to use this class you need this code

Dim DB As New DataBase
DB.EmpID="E001"
DB.EmpName="Jack"
DB.EmpSalary=10000
DB.Save

0
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 800 total points
ID: 17119809
0
 

Author Comment

by:OsirisJa
ID: 17120043
:) Thank you both. I have solved my current problem and learned a lot of new things!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question