Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6333041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:23:05+00:00 2026-05-24T18:23:05+00:00

I am trying to learn how to do this .NET frameworks for my job

  • 0

I am trying to learn how to do this .NET frameworks for my job and what not….. I can’t figure why it isn’t working.

Error occurs here:


myCommand.Connection.Open()

I am assuming it is because of how I am doing….


Dim idbox As TextBox = E.Item.Cells(numCols - 1).Controls(0)
myCommand.Parameters("@Id").Value = Integer.Parse(idbox.Text)

Source:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<script language="VB" runat="server">
   Dim myConnection As SqlConnection
   '  Create a connection to the "pubs" SQL database located on the 
   ' local computer. 
    Sub Page_Load(Src As Object, E As EventArgs)
        If Session("Admin") <> True Then
            Response.Redirect("login.aspx")
        Else

            Dim myConnection As SqlConnection = New SqlConnection("CONNECTION INFO")
            ' Determine whether this page is a postback. If it is not a
            ' postback, call BindGrid.
            If Not IsPostBack Then
                Dim dbconn As OleDbConnection
                Dim sql As String
                Dim dbcomm As OleDbCommand
                Dim dbread As OleDbDataReader
                dbconn = New OleDbConnection("CONNECTION INFO")
                dbconn.Open()
                sql = "SELECT Name FROM TestData"
                dbcomm = New OleDbCommand(sql, dbconn)
                dbread = dbcomm.ExecuteReader()
                DropDownList1.Items.Clear()
                While dbread.Read
                    DropDownList1.Items.Add(dbread(0))
                End While
                dbread.Close()
                dbconn.Close()
                BindGrid()
            End If
        End If
    End Sub

   ' Create an index to the DataGrid row that is clicked and 
   ' call BindGrid.
   Sub MyDataGrid_Edit(sender As Object, E As DataGridCommandEventArgs)
      MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
      BindGrid()
   End Sub

   ' Cancel resets the index to the row's previous settings.
   Sub MyDataGrid_Cancel(sender As Object, E As DataGridCommandEventArgs)
      MyDataGrid.EditItemIndex = -1
      BindGrid()
   End Sub

   ' When the  Update link is clicked, build a SQL UPDATE command,    
   ' connect to the database, update the row's information in the 
   ' database, and rebind the DataGrid to show the updated information.
   Public Sub MyDataGrid_Update(sender As Object, _
      E As DataGridCommandEventArgs)
        Dim updateCmd As String = "UPDATE TestData SET AdoptedNum = @AdoptedNum, PrecinctNum = @PrecinctNum WHERE Id = @Id"
        Dim myCommand As SqlCommand = New SqlCommand(updateCmd, myConnection)
        myCommand.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar))
        myCommand.Parameters.Add(New SqlParameter("@PrecinctNum", SqlDbType.Int))
        myCommand.Parameters.Add(New SqlParameter("@AdoptedNum", SqlDbType.Int))
        myCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.Int))

      ' Initialize the SqlCommand "@ID" parameter to the ID of the row 
        ' that must be clicked.
        Dim numCols As Integer = E.Item.Cells.Count
        Dim i As Integer
        Dim colvalue As String
        Dim txtBox As TextBox
        Dim idbox As TextBox = E.Item.Cells(numCols - 1).Controls(0)
        myCommand.Parameters("@Id").Value = Integer.Parse(idbox.Text)
      ' Create an array of column names.
        Dim cols() As String = {"@Name", "@PrecinctNum", "@AdoptedNum", "@Id"}
      ' Skipping the first, second, and last columns, iterate through the 
      ' columns, checking for empty values. If an empty value is found, 
      '  display a message box. Also initialize the SqlCommand 
      ' parameter values.
        For i = 2 To numCols - 1
            txtBox = E.Item.Cells(i).Controls(0)
            colvalue = txtBox.Text
            If (i < numCols And colvalue = "") Then
                Message.InnerHtml = "ERROR: Null values not allowed for " _
                   & "Author ID, Name or Phone"
                Message.Style("color") = "red"
                Exit Sub
            End If
            myCommand.Parameters(cols(i - 1)).Value = colvalue
        Next i

        ' Connect to the database and update the information.
        myCommand.Connection.Open()
        ' Test  whether the data was updated, and display the 
        ' appropriate message to the user.
        Try
            myCommand.ExecuteNonQuery()
            Message.InnerHtml = "<b>Record Updated.</b><br>"
            MyDataGrid.EditItemIndex = -1
        Catch ex As SqlException
            If ex.Number = 2627 Then
                Message.InnerHtml = "ERROR: A record already exists" _
                   & " with the same primary key"
            Else
                Message.InnerHtml = "ERROR: Could not update record," _
                   & " please ensure the fields are correctly filled out."
                Message.Style("color") = "red"
            End If
        End Try

        ' Close the connection.
        myCommand.Connection.Close()
        ' Rebind the DataGrid to show the updated information.
        BindGrid()
   End Sub

   ' The BindGrid procedure connects to the database and implements
    ' a SQL SELECT query to get all the data in the "Authors" tablea.
   Public Sub BindGrid() 
        Dim myConnection As SqlConnection = _
           New SqlConnection("CONNECTION INFO")
        Dim myCommand As SqlDataAdapter = New SqlDataAdapter("SELECT *" _
           & " FROM TestData WHERE Name='" & DropDownList1.SelectedValue & "'", myConnection)
      Dim ds As DataSet= New DataSet()
      myCommand.Fill(ds)
      MyDataGrid.DataSource = ds
        MyDataGrid.DataBind()
   End Sub

    Protected Sub MyDataGrid_SelectedIndexChanged(sender As Object, e As System.EventArgs)

    End Sub

    Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
        BindGrid()
    End Sub
</script>

<body style="font: 10pt verdana">
   <form id="Form1" runat="server"><center>
      <h3><font face="Verdana">Updating a Row of Data.</font></h3>
      <span id="Message" EnableViewState="false" 
         style="font:arial 11pt;" runat="server"/><p>
             <asp:DropDownList ID="DropDownList1"  runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
             </asp:DropDownList>
      <ASP:DataGrid id="MyDataGrid" runat="server"
         Width="800"
         BackColor="#ccccff" 
         BorderColor="black"
         ShowFooter="false" 
         CellPadding=3 
         CellSpacing="0"
         Font-Name="Verdana"
         Font-Size="8pt"
         HeaderStyle-BackColor="#aaaadd"
         OnEditCommand="MyDataGrid_Edit"
         OnCancelCommand="MyDataGrid_Cancel"
         OnUpdateCommand="MyDataGrid_Update"
      >
      <Columns>
         <ASP:EditCommandColumn EditText="Edit" CancelText="Cancel" 
            UpdateText="Update"/>
      </Columns>
   </ASP:DataGrid>
   </center>
</form>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:23:06+00:00Added an answer on May 24, 2026 at 6:23 pm

    I suspect the problem is you define but never initialize the instance variable myConnection. You define and instantiate a local variable of the same name within the Page_Load function, but that is a distinct and different object than your instance variable.

    In your Page_Load, if you change this:

    Dim myConnection As SqlConnection = New SqlConnection("CONNECTION INFO")
    

    to this:

    myConnection As SqlConnection = New SqlConnection("CONNECTION INFO")
    

    then your instance variable should be initialized and ready for use in your MyDataGrid_Update event handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn ASP.NET MVC and I hit this problem: I have
I am trying to learn Asp.net Mvc so I am trying out this Tutorial.
I'm trying to learn ASP.NET MVC, so I did as test project this where
I've been on a tear lately trying to learn everything I can about .Net
I'm working through examples in a book trying to learn ASP.NET, and I've stumbled
I'm trying to learn this basic thing about processors that should be taught in
I am learning C++. I am trying to learn this dynamic memory allocation. In
I'm trying to learn about this feature of javascript I keep seeing in code,
I am trying to learn and writting this part of code. while testing few
Lately, I've been trying to learn C++ from this website . Unfortunately whenever I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.