• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

SQL DATA BASE COLUMNS

I ahve an SQL data base with 15 columns.   The data base is updated by two different forms with different infomation; however, information that is needed to be drawn into a gridview.

My issue is the forms do not insert data into the columns utilized by the other form which is throwing the following error:

Column name or number of supplied values does not match table definition.

Is there a work around for this?
0
BOEING39
Asked:
BOEING39
  • 4
  • 3
  • 3
1 Solution
 
Steve WalesSenior Database AdministratorCommented:
You'll need to modify the forms / programs - whatever the code is that updates the table.

Make sure that the code for the inserts is inserting the correct number of columns, with the correct data types.

If you want to be sure, you can specify the column names in the insert statement:

insert into mytable(col1, col2, col3)
values (data_for_col1, data_for_col1, data_for_col3);
0
 
BOEING39Author Commented:
The columns are correct that are being inserted.   I am new to SQL; however, I guess my question here is does the number of table categories or columns have to match the form?   The table contains 15 columns.  

Form 1 uses 10 of those and Form 2 uses the other 5.    When I delete the 5 columns used on the 2nd form from SQL Server , Form 1 work just fine.

In MS Access this made no difference.  I could have as many columns in the table as I needed which were not all used with each individual form.
0
 
Steve WalesSenior Database AdministratorCommented:
Well, depends how your columns are defined.

If you have the 5 that aren't used defined to allow nulls, you could just insert the 10 that form 1 uses, and have the others as nulls.

Alternatively, if the table is defined so that everything is not null, then you will need to provide a value even if it's just a space or a zero.

Let's say I have a table that looks like this:

create table tab1 (keycol int, col1 char(10), col2 char(10))

If form 1 uses the key column and col1 and form 2 uses the key column and col2, I could do this:

Form 1:
insert into tab1 (keycol, col1) values (@variable1, @variable2)

Then on Form 2:
insert into tab1 (keycol, col2) values (@variable1, @variable2)

Since the columns aren't defined as not null, the 2 values for each insert are inserted as stated, the unspecified column is null.

However, since you're getting a column error, I'm guess that's not the issue.  The columns are probably defined as not null.

Can you provide the table definition and the insert code from the 2 forms ?  (mask table and column names with generic tab1, col1 type syntax if you prefer.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
corvetteguruCommented:
Are you using stored procedures or scripted SQL?

This error means, simply put, the number of items you are updating does not match the number that need to be updated.

There are two causes, the above mentioned no nulls field or the proc is wrong.

I tend to agree that the null fields is the problem.
0
 
BOEING39Author Commented:
When asking original question I explain the defaultMCO below shares the same columns in some cases.   I have attached the code behind for both forms as well as the server setup.
Key.doc
default.aspx.cs
defaultMCO.aspx.cs
0
 
BOEING39Author Commented:
The only no null field is ID.....
0
 
Steve WalesSenior Database AdministratorCommented:
Problem is in defaultMCO.aspx.cs

There are 15 columns in the table.  You're inserting 12 values:

        cmd.CommandText = ("INSERT into AOS VALUES (@AOSdate,@Sta,@Ship,@ATA,@InFLt,@ArrTime,@Status,@MCOissuedBy,@History,@ETR,@Reason1,@MCOInst)");

        cmd.Parameters.AddWithValue("@AOSdate", AOSdate.Text);
        cmd.Parameters.AddWithValue("@Sta", Sta.SelectedValue);
        cmd.Parameters.AddWithValue("@Ship", Ship.SelectedValue);
        cmd.Parameters.AddWithValue("@ATA", ATA.SelectedValue);
        cmd.Parameters.AddWithValue("@InFlt", InFlt.Text);
        cmd.Parameters.AddWithValue("@ArrTime", ArrTime.Text);
        cmd.Parameters.AddWithValue("@Status", Status.SelectedValue);
        cmd.Parameters.AddWithValue("@MCOissuedBy", MCOissuedBy.SelectedValue);
        cmd.Parameters.AddWithValue("@History", History.SelectedValue);
        cmd.Parameters.AddWithValue("@ETR", ETR.SelectedValue);
        cmd.Parameters.AddWithValue("@Reason1", Reason1.Text);
        cmd.Parameters.AddWithValue("@MCOInst", MCOInst.Text);


Since the table is not allowing nulls, you are required to specify all 15 values.

Also since you're not specifying any column names, you need to have the 15 columns in the same order in the insert statement as they appear in the table.  These 12 values are all jumbled up.

You can change up the order if you specify the columns names (like I mentioned in one of my earlier replies).

Make the insert look like the one in the other form (Correct order, specifying all values) and your problem should go away.
0
 
corvetteguruCommented:
I see the problem...

You have this insert statement:

INSERT into AOS VALUES (@AOSdate,@Sta,@Ship,@ATA,@InFLt,@ArrTime,@Status,@MCOissuedBy,@History,@ETR,@Reason1,@MCOInst)

Do this:

INSERT into AOS
(AOSdate,Sta,Ship,ATA,InFLt,ArrTime,Status,MCOissuedBy,History,ETR,Reason1,MCOInst)
VALUES (@AOSdate,@Sta,@Ship,@ATA,@InFLt,@ArrTime,@Status,@MCOissuedBy,@History,@ETR,@Reason1,@MCOInst)

Using the field list will stop errors like your's...
0
 
BOEING39Author Commented:
Corvetteguru:


Right on the mark.    Thank you.

cmd.CommandText = ("INSERT into AOS (AOSdate,Ship,ArrTime,InFLt,History,ETRDate,ETRTime,Sta,Reason1,Reason2,Parts,Updated,ATA,Status,ETR) VALUES (@AOSdate,@Ship,@ArrTime,@InFlt,@History,@ETRDate,@EtrTime,@Sta,@Reason1,@Reason2,@Parts,@Updated,@ATA,@Status,@ETR)");
0
 
corvetteguruCommented:
You are most welcome! Now... enjoy your Holiday! :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now