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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:54:46+00:00 2026-06-07T21:54:46+00:00

I got a question to ask here. Why can’t my data be inserted into

  • 0

I got a question to ask here. Why can’t my data be inserted into my database? I’ve already create a table in SQL Server. Here is my line of code for your reference.

protected void Button2_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Insert into CarTab(Brand,Model,Plate,Color,Service) Values (@brand,@model,@plate,@color,@year,@service)",conn);

    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@brand", Label1.Text);
    cmd.Parameters.AddWithValue("@model", Label2.Text);
    cmd.Parameters.AddWithValue("@plate", Label3.Text);
    cmd.Parameters.AddWithValue("@color", Label4.Text);
    cmd.Parameters.AddWithValue("@year", Label5.Text);
    cmd.Parameters.AddWithValue("@service", Label6.Text);

    conn.Open();
    cmd.ExecuteNonQuery();
}

Is there something wrong with my code? Pls help me. Im stuck over here. Thank you.

  • 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-06-07T21:54:48+00:00Added an answer on June 7, 2026 at 9:54 pm

    It looks like you are missing Year in your command statement:

    SqlCommand cmd = new SqlCommand(
    "Insert into CarTab(Brand,Model,Plate,Color,Year,Service) 
        Values (@brand,@model,@plate,@color,@year,@service)",
    conn);
    

    You are supplying 6 parameters but only 5 columns.

    Are you getting an error message? Usually, there will be an error message with some information; it usually looks something like this:

    There are fewer columns in the INSERT statement than values specified
    in the VALUES clause. The number of values in the VALUES clause must
    match the number of columns specified in the INSERT statement.

    EDIT.

    If you are still having problems, the next thing to do is to look at the actual error message. Let’s refactor your code slightly:

    using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
    {
        using(SqlCommand cmd = new SqlCommand("Insert into CarTab(Brand,Model,Plate,Color,Service) Values (@brand,@model,@plate,@color,@year,@service)",conn))
        {
    
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@brand", Label1.Text);
            cmd.Parameters.AddWithValue("@model", Label2.Text);
            cmd.Parameters.AddWithValue("@plate", Label3.Text);
            cmd.Parameters.AddWithValue("@color", Label4.Text);
            cmd.Parameters.AddWithValue("@year", Label5.Text);
            cmd.Parameters.AddWithValue("@service", Label6.Text);
    
            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch(Exception e)
            {
                string errorMessage = e.Message;
                //Set a label on the client to see the error message, or pause in the debugger and examine the property here.
                //throw;
            }
        }
    }
    

    If you are seeing no error at all, and that’s what puzzled me initially, then it might actually be because your event handler isn’t wired up; sometimes, the designer will unwire event handlers meaning your click and the actually code become disassociated and never run.

    Check you have:

    Button2.Click += Button2_Click; 
    

    In InitializeComponent or in the constructor or Load events.

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

Sidebar

Related Questions

Hi i got a question to ask u all Here is my codding string
just want to ask anyone here can solve this problem? I want to create
Another slightly non-technical question, but I couldn't decide whether to ask here or on
I've got a relatively simple Cocoa on Mac OS X 10.6 question to ask.
I have tried to ask a variant of this question before. I got some
I've got the following question to ask: How do you compile taglib with an
I asked a similar question earlier here , and it got me down the
I have got a lengthy question to ask. First of all Im still very
Hi i got a very noob question to ask . I am using http
I am here to ask you a basic question about jdom. I am trying

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.