Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am inserting data into the sqlserver data table. and i have many parameters. if i fill the values and click on the insert button so values are inserting and going into the table properly. but if i am inserting only few values means not all. so it gives me an error.
i want if i click on the button without any value so it should not give me an error.
please guide me how to solve this condition with my these codes.

What I have tried:

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True");
           con.Open();
           string query = "insert into itemmaster(itemcode, name, salesprice, salestax,profit, quantityonhand) values(@itemcode, @name, @salesprice,@profit,@salestax,  @quantityonhand)";
           SqlCommand cmd = new SqlCommand(query, con);


           cmd.Parameters.AddWithValue("@itemcode", textBox1.Text);
           cmd.Parameters.AddWithValue("@name", textBox2.Text);
           cmd.Parameters.AddWithValue("@salesprice", textBox3.Text);
           cmd.Parameters.AddWithValue("@salestax", textBox4.Text);
           cmd.Parameters.AddWithValue("@profit", textBox5.Text);
           cmd.Parameters.AddWithValue("@quantityonhand", textBox6.Text);
           cmd.ExecuteNonQuery();
           MessageBox.Show("Saved");
           con.Close();
Posted
Updated 30-Apr-17 19:50pm
Comments
Nirav Prabtani 2-May-17 2:19am    
What kind of error is there ? Can you please share ?

1 solution

First of all you need to check your database table schema, if all the columns are null or having any default value specified then only your goal is achieved, means with out entering anything and clicking on button your blank record is created.

here you are using itemmaster table, so if its schmea is allowing null value/ default value to columns itemcode, name, salesprice, salestax,profit, quantityonhand then you can insert a default value/null value record to your table.

can you please share your table schema, and also don't forget to updated your error details, like what error message you are getting while inserting a blank row.


UPDATES

As you have shared your database table schema as per below script

SQL
CREATE TABLE [dbo].[itemmaster](
[itemcode] [int] NOT NULL,
[name] [varchar](50) NULL,
[salesprice] [decimal](18, 0) NULL,
[salestax] [decimal](18, 0) NULL,
[profit] [decimal](18, 0) NULL,
[quantityonhand] [decimal](18, 0) NULL,
PRIMARY KEY CLUSTERED 
(
[itemcode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


You have set itemcode as primary key with out identity column, so you need to provide value for that column while inserting record. Rest of the columns are allowed null, so you can either specified value for it or not that work to insert the data to your itemmaster table.

Or As per the below script you can modify your table schema and define itemcode as Identity Column with Primary Key, then database will automatically increment the next identity value and insert it to your table while inserting the records.

SQL
/****** Object:  Table [dbo].[itemmaster]    Script Date: 5/1/2017 8:08:10 PM ******/
DROP TABLE [dbo].[itemmaster]
GO

/****** Object:  Table [dbo].[itemmaster]    Script Date: 5/1/2017 8:08:10 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[itemmaster](
	[itemcode] [int] IDENTITY(1,1) NOT NULL,
	[name] [varchar](50) NULL,
	[salesprice] [decimal](18, 0) NULL,
	[salestax] [decimal](18, 0) NULL,
	[profit] [decimal](18, 0) NULL,
	[quantityonhand] [decimal](18, 0) NULL,
 CONSTRAINT [PK__itemmast__986EE407B02D1FB9] PRIMARY KEY CLUSTERED 
(
	[itemcode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO



Suggesion: please do not use inline query in your application, it will allow SQL Injection[^] to be injected to your application.
Instead using inline query use store procedure with in your application like this Store Procedure Demo[^]
 
Share this answer
 
v3
Comments
faizy001 1-May-17 1:59am    
CREATE TABLE [dbo].[itemmaster](
[itemcode] [int] NOT NULL,
[name] [varchar](50) NULL,
[salesprice] [decimal](18, 0) NULL,
[salestax] [decimal](18, 0) NULL,
[profit] [decimal](18, 0) NULL,
[quantityonhand] [decimal](18, 0) NULL,
PRIMARY KEY CLUSTERED
(
[itemcode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
faizy001 1-May-17 2:06am    
i am sending my table structure. please check
Tejas Vaishnav 1-May-17 11:13am    
can you please check my answer i have updated it accordingly, and don't forget to accept it if it solve your problem
CHill60 1-May-17 11:38am    
At least the OP used a parameterised query to help prevent SQL injection

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900