Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i create two radiobutton New and old and i want if user don't check any one the value saved null in database.

this is my code :-

C#
for (int i = 0; i < dgv_Add_job_card.Rows.Count; i++)
 {

     s = "insert into Auto_Part_Details (Items_No, Job_Card, Value_Part, Auto_Parts_id, Repair_id, New_Old_Part, Provider_id, Installation_id, Notes) values (@Items_No, @Job_Card, @Value_Part,@Auto_Parts_id , @Repair_id,@New_Old_Part , @Provider_id, @Installation_id , @Notes)";
     sCommand = new SqlCommand(s, con);
     sCommand.Parameters.AddWithValue("@Items_No", dgv_Add_job_card.Rows[i].Cells[0].Value);
     sCommand.Parameters.AddWithValue("@Job_Card", dgv_Add_job_card.Rows[i].Cells[1].Value);
     sCommand.Parameters.AddWithValue("@Value_Part", dgv_Add_job_card.Rows[i].Cells[2].Value);
     sCommand.Parameters.AddWithValue("@New_Old_Part", dgv_Add_job_card.Rows[i].Cells[5].Value);
     sCommand.Parameters.AddWithValue("@Notes", dgv_Add_job_card.Rows[i].Cells[8].Value);
     sCommand.Parameters.AddWithValue("@Auto_Parts_id", dgv_Add_job_card.Rows[i].Cells[9].Value);
     sCommand.Parameters.AddWithValue("@Repair_id", dgv_Add_job_card.Rows[i].Cells[10].Value);
     sCommand.Parameters.AddWithValue("@Provider_id", dgv_Add_job_card.Rows[i].Cells[11].Value);
     sCommand.Parameters.AddWithValue("@Installation_id", dgv_Add_job_card.Rows[i].Cells[12].Value);
     sCommand.ExecuteNonQuery();

 }


What I have tried:

this value show in gridview and i save it from gridview to database
Posted
Updated 23-Mar-16 22:31pm
Comments
dan!sh 24-Mar-16 4:11am    
What's happening with this code?
Member 12244977 24-Mar-16 4:27am    
i have two radiobutton this radio button take datatybe bit 0 and 1 when i dont check on any one the value save 0 but i want save it null not 0
Member 12244977 24-Mar-16 4:27am    
how i do this in insert query

I'd recommend that you don't - it won't work too well when you come to read the values back either.
But...it's possible.
All you have to do is test the Checkbox.Checked state and either pass 1 or DBNull.Value as the parameter if it's true of false respectively.
 
Share this answer
 
The SQL Server bit datatype can be 0,1 or NULL.
See: bit (Transact-SQL)[^]

But I think your problem is that in the database you have defined your bit field as NOT NULL.
 
Share this answer
 

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