Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Inserting from DG

  Asked By: Madison    Date: Apr 08    Category: Asp.net    Views: 724
  

I know this might not be on the topic if not please advise off list.
On one of my DG commands I am inserting values into the db

Table name StaticIP

Datatype Col name

Decimal ID à just for auto increment or unique ID

Varchar(50) Ipaddress à to store the ip address (is there abetter datatype to use?)

NVarchar(50) Ipowner à basically description of who the ip belongs to(don't even know why I chose this datatype ;-) whats the best for this?)

Now I have a insert query like the following:



INSERT INTO StaticIPAdd (IPaddress ,IPOwner)

VALUES (20.20.30.1, 'A description of carlos machine.')

But I get an error:

Cannot insert the value NULL into column 'ID', table 'ITConfig.dbo.StaticIPAdd'; column does not allow nulls. INSERT fails.

The statement has been terminated.

Now isn't the ID col suppose to auto increment, or is this different for an insert if so how do I auto increment it?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Daya Sharma     Answered On: Apr 08

i'm not sure but try one of those:

1) try to convert that IP to a string with a variable and use that upping your data


Dim myIP as string = Ctype("20.20.30.1",String)

INSERT INTO StaticIPAdd (IPaddress ,IPOwner)

VALUES (myIP, 'A description  of carlos machine.')

2) if u are using SQL Server go to table  design and set option identity to NO
As said, i'm not very sure about this but it could be...

 
Answer #2    Answered By: Viren Rajput     Answered On: Apr 08

In Enterprise Manager you right click the table  and choose Design Table. Then for the ID column  you choose set the Identity property to yes. It's better to use a integer (int) datatype  for your "autoincrement" column because it will never hold decimal values  just whole numbers.
The difference between varchar and nvarchar is that nvarchar will allow more international characters than varchar (not useful for an IP address).
You should watch out with the field length of the (n)varchar column as it is difficult to predict the maximum field length users will input. 50 characters is really small. You don't want your application to give an error  when it occurs. If you have no idea what is a good length for your field you can also use the (n)text field which autoincrements.

 
Answer #3    Answered By: Cheryl Murphy     Answered On: Apr 08

OK good old books on line helped here after doing some reading I found out for it to auto  increment from a insert  statement you have to set :

SET IDENTITY_INSERT <YOURTABLENAME> OFF (as scoopie suggested)

I did so and it works perfectly, just something interesting for you all.

 
Didn't find what you were looking for? Find more on Inserting from DG Or get search suggestion and latest updates.




Tagged: