Did not find any error but data is not updating in database and displaying error on first load is
There is no row at position 0
and data displaying in gridview below.
But when load from menu data is displaying in textbox's.
Update Button Code
Protected Sub taxsubmit_Click(sender As Object, e As ImageClickEventArgs) Handles taxsubmit.Click
Using con As SqlConnection = New SqlConnection(strConnString)
Using cmd As SqlCommand = New SqlCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "UPDATE [Deduction] SET [IncomeTax] = @IncomeTax, [SalesTax] = @SalesTax, [ServiceTax] = @ServiceTax, [LabourCess] = @LabourCess, [SocityTax] = @SocityTax, [ESIC] = @ESIC, [EPF] = @EPF, [Security] = @Security, [FinYear] = @FinYear, [Condition1] = @Condition1, [Condition2] = @Condition2, [Condition3] = @Condition3, [CompID] = " + Session("Companydetl") + " WHERE [DedID] = " + dedid + ""
cmd.Parameters.AddWithValue("@IncomeTax", Income_Tax.Text)
cmd.Parameters.AddWithValue("@SalesTax", Sales.Text)
cmd.Parameters.AddWithValue("@ServiceTax", Service.Text)
cmd.Parameters.AddWithValue("@LabourCess", Labour_Cess.Text)
cmd.Parameters.AddWithValue("@SocityTax", Society.Text)
cmd.Parameters.AddWithValue("@ESIC", ESIC.Text)
cmd.Parameters.AddWithValue("@EPF", EPF.Text)
cmd.Parameters.AddWithValue("@Security", Security.Text)
cmd.Parameters.AddWithValue("@FinYear", Fin_Year.SelectedValue)
cmd.Parameters.AddWithValue("@Condition1", Cond1.Text)
cmd.Parameters.AddWithValue("@Condition2", Cond2.Text)
cmd.Parameters.AddWithValue("@Condition3", Cond3.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Data Display Code
Using con As SqlConnection = New SqlConnection(strConnString)
Using cmd As SqlCommand = New SqlCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select DedID, IncomeTax, SalesTax, ServiceTax, LabourCess, SocityTax, ESIC, EPF,
Security, FinYear, Condition1, Condition2, Condition3 from Deduction where CompID = '" + Session("Companydetl") + "'"
Dim dt As New DataTable()
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
Try
dt.Load(reader)
Income_Tax.Text = dt.Rows(0).Item("IncomeTax").ToString.Trim()
Labour_Cess.Text = dt.Rows(0).Item("LabourCess").ToString.Trim()
ESIC.Text = dt.Rows(0).Item("ESIC").ToString.Trim()
EPF.Text = dt.Rows(0).Item("EPF").ToString.Trim()
Society.Text = dt.Rows(0).Item("SocityTax").ToString.Trim()
Service.Text = dt.Rows(0).Item("ServiceTax").ToString.Trim()
Sales.Text = dt.Rows(0).Item("SalesTax").ToString.Trim()
Security.Text = dt.Rows(0).Item("Security").ToString.Trim()
Fin_Year.SelectedValue = dt.Rows(0).Item("FinYear").ToString.Trim()
Cond1.Text = dt.Rows(0).Item("Condition1").ToString.Trim()
Cond2.Text = dt.Rows(0).Item("Condition2").ToString.Trim()
Cond3.Text = dt.Rows(0).Item("Condition3").ToString.Trim()
dedid = dt.Rows(0).Item("DedID").ToString.Trim()
con.Close()
Catch ex As Exception
MsgBox(ex.Message, vbOKOnly, "Error")
End Try
End Using
End Using
Image on direct load of page after login.
Image After loading page from the above menu.
as am new to this please help.
Clickevent handler, where are you setting the value ofdedid? That's the first place I'd look; if your query isn't returning any results, there's no record to update. Also, your SQL statement is vulnerable to an injection attack. You'll want to use query parameters for the entire statement, not just most of it.[CompID]and[DedID]. Concatenating the string as you are now creates the vulnerability. Search: Prevent SQL Injection