Hello,

i need help to do the insert inside repeater, but when i debug the stock_code inside sql command is not declared. what should i do?

Protected Sub repeaterAlert_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles repeaterAlert.ItemDataBound
        Try
            stockCodeheader.Text = " Stock Code "
            plcNameheader.Text = " PLC Name "
            boardheader.Text = " Board "
            sectorheader.Text = " Sector "
            subSectorheader.Text = " Sub Sector "
            yearheader.Text = " Year "
            total.Text = " DSO "

            '** Declare Session **'
            If Session("tempara1") = "" Then
                DSO1 = e.Item.DataItem("para1").ToString
            Else
                DSO1 = Session("tempara1")
            End If
            '*********************'

            Dim stockCode As Label = CType(e.Item.FindControl("stockCode"), Label)
            If Not IsNothing(stockCode) Then
                stockCode.Text = e.Item.DataItem("stock_code").ToString
            End If

            Dim plcName As Label = CType(e.Item.FindControl("plcName"), Label)
            If Not IsNothing(plcName) Then
                plcName.Text = e.Item.DataItem("current_name").ToString
            Else
            End If

            Dim board As Label = CType(e.Item.FindControl("board"), Label)
            If Not IsNothing(board) Then
                board.Text = e.Item.DataItem("board_name").ToString
            Else
            End If

            Dim sector As Label = CType(e.Item.FindControl("sector"), Label)
            If Not IsNothing(sector) Then
                sector.Text = e.Item.DataItem("sector_name").ToString
            Else
            End If

            Dim subSector As Label = CType(e.Item.FindControl("subSector"), Label)
            If Not IsNothing(subSector) Then
                subSector.Text = e.Item.DataItem("sub_sector_name").ToString
            Else
            End If

            Dim year As Label = CType(e.Item.FindControl("year"), Label)
            If Not IsNothing(year) Then
                year.Text = e.Item.DataItem("year_id").ToString
            Else
            End If

            '** alert's colour trigger **'

            Dim total1 As Label = CType(e.Item.FindControl("total1"), Label)
            total1.Text = e.Item.DataItem("dso").ToString
            Dim compval As Double = total1.Text
            total1.Text = Math.Round(compval, 2)

            If total1.Text >= DSO1 Then
                total1.BackColor = Drawing.Color.Red
            End If

            If Session("tempara1") = "" Then
                '** insert to fistdb **'

                Dim command As New MySqlCommand("INSERT INTO company_alert (stock_code, year_id, alert_value, created_by, created_date) VALUES (@stock_code, @year_id, @alert_value, @created_by, @created_date)", conn)

                command.Parameters.AddWithValue("@stock_code", stockCode.Text)
                command.Parameters.AddWithValue("@year_id", year.Text)
                command.Parameters.AddWithValue("@alert_value", total1.Text)
                command.Parameters.AddWithValue("@created_by", HttpContext.Current.Session("userid"))
                command.Parameters.AddWithValue("@created_date", Format(Now(), "yyyy-MM-dd HH:mm"))

                command.ExecuteNonQuery()
            End If

        Catch ex As Exception

        End Try

    End Sub

Dani AI

Generated

Quick summary: the most common reasons the rows never appear are that an exception is being swallowed by the empty Catch block, the MySQL connection object is not open or not the same instance passed to the command, controls returned by FindControl are Nothing (so parameters are null), or the DB is rejecting the insert (constraints/permissions). 's insert is inside ItemDataBound; 's stored-proc suggestion is valid for maintainability, but first make runtime problems visible and fix the immediate resource/parameter issues.

Immediate checklist to diagnose and fix:

  • Stop swallowing exceptions. Replace the empty Catch with logging (Trace.Warn, Debug.WriteLine or write to a log file) so the exact error appears.
  • Use Using blocks for MySqlConnection/MySqlCommand and verify cn.Open() is called on the same connection instance given to the command.
  • Check every FindControl result before reading .Text (If ctrl IsNot Nothing Then ...). A NullReference will prevent parameters from being set.
  • Avoid AddWithValue where type inference can be wrong. Add typed parameters (MySqlDbType, size) and pass DateTime.Now for datetime fields instead of a formatted string.
  • Run the same INSERT manually in a MySQL client and also test the insert from a simple button click (outside the repeater) to isolate binding logic from DB issues.

Repeater-specific notes: ItemDataBound runs during DataBind and will fire on every bind. Wrap DataBind inside If Not Page.IsPostBack or move the insert into a controlled event (Save button or Repeater.ItemCommand) to avoid unexpected/duplicate writes. If duplicate rows are a concern, handle idempotency with a stored procedure or use INSERT ... ON DUPLICATE KEY UPDATE.

Safe pattern example (stored-proc call, logging and typed parameters):

Try
    Using cn As New MySqlConnection(connString)
        cn.Open()
        Using cmd As New MySqlCommand("sp_InsertCompanyAlert", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@p_stock_code", MySqlDbType.VarChar, 50).Value = stockCodeText
            cmd.Parameters.Add("@p_year_id", MySqlDbType.Int32).Value = CInt(yearText)
            cmd.Parameters.Add("@p_alert_value", MySqlDbType.Double).Value = CDbl(totalText)
            cmd.Parameters.Add("@p_created_by", MySqlDbType.Int32).Value = CInt(HttpContext.Current.Session("userid"))
            cmd.Parameters.Add("@p_created_date", MySqlDbType.DateTime).Value = DateTime.Now
            cmd.ExecuteNonQuery()
        End Using
    End Using
Catch ex As Exception
    Trace.Warn("DB Insert failed", ex.ToString())
End Try

Final checks: confirm the DB user has INSERT rights, verify table constraints/unique keys, and collect the exception text if the problem persists.

Recommended Answers

All 5 Replies

What is the error? or what is the problem? plz explain

the problem is all the data generated did not insert to the database.

Write the procedure at back end and pass the parameters from front end. Its always easy to manage the code.

i don't understand. can't u give me example? please.

Search in google for stored procedure and see how to use that in ur app.

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.