Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
VB.NET
Dim cn As New SqlConnection
       Dim cmd As New SqlCommand

       Dim str As String


       cn.ConnectionString = "Data Source=PARTH-PC;Initial Catalog=db_hba;Integrated Security=True"
       Dim ca As Integer
 Try

            cn.Open()
 str = "insert into EmMaster(EmMaster_EmpCode,EmMaster_EmpName,EmMaster_EmDesig,"
            str += "EmMaster_EmpPort,EmMaster_EmpDivi,EmMaster_SubDivi,EmMaster_EmplDob,EmMaster_EmplDoj,EmMaster_EmplDor,"
            str += "EmMaster_Service,EmMaster_EmplMob,EmMaster_EmEmail,EmMaster_PaScale) values ('" + txtEmpCode.Text + "','" + txtName.Text + "','" + txtDesignation.Text + "','" + txtPort.Text + "','" + txtDivision.Text + "','" + txtSubDivision.Text + "','" + txtDOB.SelectedDate.ToString + "','" + txtDOJ.SelectedDate.ToString + "','" + txtDOR.SelectedDate.ToString + "','" + txtTotalService.Text + "','" + txtMobile.Text + "','" + txtEmail.Text + "','" + txtPayScale.Text + "')"

  cmd = New SqlCommand(str, cn)
            ca = cmd.ExecuteNonQuery

            MessageBox.Show(ca)

        Catch ex As Exception
            MessageBox.Show("could not insert record")
            cn.Close()
        End Try
Posted
Comments
Michael_Davies 28-Jan-16 3:16am    
What is the exact error message, display the error using ex.Message.

Do not use a constructed string for your SQL statement, use a parameterised string and add the parameters and values, besides being difficult to spot errors in the string you are prone to SQL injection. For instance if _EmpName was O'Connor the single quote in the text in the texbox would close the SQL string and make the rest garbage and cause failure, you would not know as you do not look at the error message.

Use the debugger and examine the string.

1 solution

Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The chances are that that will fix your problem.
VB
str = "insert into EmMaster(EmMaster_EmpCode, EmMaster_EmpName, EmMaster_EmDesig, EmMaster_EmpPort, EmMaster_EmpDivi, EmMaster_SubDivi, EmMaster_EmplDob, EmMaster_EmplDoj, EmMaster_EmplDor, EmMaster_Service, EmMaster_EmplMob, EmMaster_EmEmail, EmMaster_PaScale) values (@COD, @NAM, @DES, @PRT, @DIV, @SDV, @DOB, @DOJ, @DOR, @SER, @MOB, @EML, @PAS)"
cmd = New SqlCommand(str, cn)
cmd.Parameters.AddWithValue("@COD", txtEmpCode.Text)
cmd.Parameters.AddWithValue("@NAM", txtName.Text)
cmd.Parameters.AddWithValue("@DES", txtDesignation.Text)
cmd.Parameters.AddWithValue("@PRT", txtPort.Text)
cmd.Parameters.AddWithValue("@DIV", txtDivision.Text)
cmd.Parameters.AddWithValue("@SDV", txtSubDivision.Text)
cmd.Parameters.AddWithValue("@DOB", txtDOB.SelectedDate)
cmd.Parameters.AddWithValue("@DOJ", txtDOJ.SelectedDate)
cmd.Parameters.AddWithValue("@DOR", txtDOR.SelectedDate)
cmd.Parameters.AddWithValue("@SER", txtTotalService.Text)
cmd.Parameters.AddWithValue("@MOB", txtMobile.Text)
cmd.Parameters.AddWithValue("@EML", txtEmail.Text)
cmd.Parameters.AddWithValue("@PAS", txtPayScale.Text)

But...do yourself a favour and dump the "EmMaster_Emp" prefix on all your columns - you don't need them and it just makes everything harder to read. And don't call everything "txt..." - particularly when it isn't a TextBox! That's just confusing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900