i need to update my data using stored procedure here is my code :

Try
            con = getconnect()
            con.Open()
           

            sqlcmd = con.CreateCommand
            sqlcmd.CommandText = "Select *  from EmployeeProfile where EmployeeID = '" & txtEmpID.Text & "'"
            daemployee.SelectCommand = cmdemployee1
            daemployee.Fill(dsemployee, "EmployeeProfile")
            If dsemployee.Tables(0).Rows.Count <> 0 Then
                txtEmpID.Text = dsemployee.Tables(0).Rows(0).Item("EmployeeID")
                txtFName.Text = dsemployee.Tables(0).Rows(0).Item("FirstName")
                txtMName.Text = dsemployee.Tables(0).Rows(0).Item("MiddleName")
                txtLName.Text = dsemployee.Tables(0).Rows(0).Item("LastName")
                txtAddress.Text = dsemployee.Tables(0).Rows(0).Item("Address")
                txtEmail = dsemployee.Tables(0).Rows(0).Item("EmailAdd")
                txtCell = dsemployee.Tables(0).Rows(0).Item("CellphoneNo")

                Dim strStoredProc2 As String = "sp_Consultant_Update_Employee"
                cmdemployee = New SqlCommand(strStoredProc2, con)
                cmdemployee.CommandType = System.Data.CommandType.StoredProcedure
                cmdemployee.Parameters.AddWithValue("@param_employeeid", EmployeeID)
                cmdemployee.CommandTimeout = 50000
                cmdemployee.ExecuteNonQuery()
            Else
                MsgBox("MALI")
            End If
            
            'cmdemployee = con.CreateCommand
            'daemployee.SelectCommand = cmdemployee
            'dtemployee = dsemployee.Tables("EmployeeProfile")

            'cmdemployee.CommandText = "UPDATE EmployeeProfile SET FirstName = '" & Trim(txtFName.Text) & "', MiddleName= '" & Trim(txtMName.Text) & "', LastName= '" & Trim(txtLName.Text) & "', Address= '" & Trim(txtAddress.Text) & "', CellphoneNo= '" & Trim(txtCell.Text) & "', EmailAdd= '" & Trim(txtEmail.Text) & "'WHERE EmployeeID= '" & Trim(txtEmpID.Text) & "'"
            check = cmdemployee.ExecuteReader.RecordsAffected
            MsgBox(check)
            If check <> 0 Then
                MsgBox("Employee Profile Successfully Edited", MsgBoxStyle.OkOnly)
            Else
                MsgBox("Fail to Edit", MsgBoxStyle.OkOnly)

            End If
            con.Close()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

then this error is appearing. "Fill: SelectCommand.Connection property has not been initialized"

then here is my code in stored procedure

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_Consultant_Update_Employee] 
	-- Add the parameters for the stored procedure here
	@param_employeeid as int,
	@param_firstname as varchar(50),
	@param_middlename as varchar(50),
	@param_lastname as varchar(50),
	@param_address as varchar(100),
	@param_email as varchar(50),
	@param_cell as varchar(20)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	--SELECT * from EmployeeProfile where EmployeeID = @param_employeeid

	update EmployeeProfile
	set 
	FirstName = @param_firstname,
	MiddleName = @param_middlename,
	LastName = @param_lastname,
	Address = @param_address,
	CellphoneNo =  @param_cell,
	EmailAdd = @param_email
	where EmployeeID = @param_employeeid
END
GO

which is wrong? my .net code or my stored procedure?
can you please explain the error "Fill: SelectCommand.Connection property has not been initialized"

thanks

Recommended Answers

All 5 Replies

Set Connection property of SqlCommand object.

..
sqlcmd.Connection=con

i already add it. but its still says the same error

I guess your code should be.

con = getconnect()

sqlcmd = con.CreateCommand
sqlcmd.CommandText = "Select *  from EmployeeProfile where EmployeeID = '" & txtEmpID.Text & "'"
sqlcmd.Connection=con

daemployee.SelectCommand = sqlcmd;
daemployee.Fill(dsemployee, "EmployeeProfile")

thanks that's help.. but this is the new error..
i think this error is regarding my stored procedure

the error is "Procedure or Function 'sp_Consultant_Update_Employee' expects parameter '@param_firstname', which was not supplied."

i already solve this problem.. thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.