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 7663853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:09:18+00:00 2026-05-31T14:09:18+00:00

I have a small DataGridView which lets the user enter data. And I’m trying

  • 0

I have a small DataGridView which lets the user enter data.

Ths is the grid

And I’m trying this code to enter the data in the DataGridView to the database.

    strQry = "INSERT INTO Emp_Fam_Details (empID, famName, famAge, famRelation, famOccup)" _
            & "VALUES (@ID, @Name, @Age, @Rel, @Occ)"
    Dim adapFam As New SqlDataAdapter
    adapFam.InsertCommand = New SqlCommand(strQry, sqlcon)

    For i As Integer = 0 To grdFamily.Rows.Count - 1
        adapFam.InsertCommand.Parameters.AddWithValue("@ID", txtEmpID.Text.Trim)
        adapFam.InsertCommand.Parameters.AddWithValue("@Name", grdFamily.Columns(0))
        adapFam.InsertCommand.Parameters.AddWithValue("@Age", grdFamily.Columns(1))
        adapFam.InsertCommand.Parameters.AddWithValue("@Rel", grdFamily.Columns(2))
        adapFam.InsertCommand.Parameters.AddWithValue("@Occ", grdFamily.Columns(3))

        result2 = adapFam.InsertCommand.ExecuteNonQuery()
    Next

When I run this, I come across this error.

This is the error

I found this solution here on SO but since I’m using Parameters not appended values, I’m having a hard time understanding how to do it with parameters.

Can anyone please show me how to do this?

Thank you all.

  • 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-31T14:09:19+00:00Added an answer on May 31, 2026 at 2:09 pm

    You cannot map columns to data. What you need to add is the row cell data of that column index

    This is wrong grdFamily.Columns(0)

    Parameter assignment within the for loop should be corrected as follows

    NOTE Please change datatype as per your table.

    adapFam.InsertCommand.Parameters.Add("@ID", SqlDbType.Int)
    adapFam.InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar)
    adapFam.InsertCommand.Parameters.Add("@Age", SqlDbType.VarChar)
    adapFam.InsertCommand.Parameters.Add("@Rel", SqlDbType.VarChar)
    adapFam.InsertCommand.Parameters.Add("@Occ", SqlDbType.VarChar)
    
    For i As Integer = 0 To grdFamily.Rows.Count - 1
      adapFam.InsertCommand.Parameters[0].Value = Convert.ToInt32(txtEmpID.Text.Trim)
      adapFam.InsertCommand.Parameters[1].Value = grdFamily.Rows[i].Cells[0].Value
      adapFam.InsertCommand.Parameters[2].Value = grdFamily.Rows[i].Cells[1].Value
      adapFam.InsertCommand.Parameters[3].Value = grdFamily.Rows[i].Cells[2].Value
      adapFam.InsertCommand.Parameters[4].Value = grdFamily.Rows[i].Cells[3].Value
    
        result2 = adapFam.InsertCommand.ExecuteNonQuery()
    Next
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using c#2.0 and WinForms. I have a datagridview control, unbound, loading small amounts
I have small page which has label, DropDownList and a submit button. <div> <asp:label
I have small database of business and their addresses. Using the Google Geocode API
I have small web app that generate PDF files as a report. I'm trying
I have small question about pdp-11(simulator), I have this command (it begins from the
I have small function which will recolor pixels in a Bitmap from a given
I have datagridviews on windows form, i am adding records to datagridview using small
Have small web page at www.peterbio.com/mom/test.htm Someone wrote the code with mouse over and
I have small PHP script which has $query = SELECT MAX(id) FROM `dbs`; //query
I have a MDI Child Form which contains a DataGridView that is updated continuously

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.