Click here to Skip to main content
15,566,625 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void btnSave_Click(object sender, RoutedEventArgs e)
       {
           try
           {
               if (isValid())
               {
                  // id1 = picUserimage.Source.ToString();
                   SqlConnection conn = new SqlConnection(ConnectionString);
                   conn.Open();
                   SqlCommand cmd = new SqlCommand("add_profile_sp", conn);
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.Parameters.AddWithValue("@company_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(txtCompanyId.Text) ? DBNull.Value : (object)txtCompanyId.Text;
                   cmd.Parameters.AddWithValue("@company_name", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtCompanyName.Text) ? DBNull.Value : (object)txtCompanyName.Text;
                   cmd.Parameters.AddWithValue("@caption", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtCaption.Text) ? DBNull.Value : (object)txtCaption.Text;
                   cmd.Parameters.AddWithValue("@address1", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtAddress1.Text) ? DBNull.Value : (object)txtAddress1.Text;
                   cmd.Parameters.AddWithValue("@address2", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtAddress2.Text) ? DBNull.Value : (object)txtAddress2.Text;
                   cmd.Parameters.AddWithValue("@mobileno", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtMobile.Text) ? DBNull.Value : (object)txtMobile.Text;
                   cmd.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtEmail.Text) ? DBNull.Value : (object)txtEmail.Text;
                   cmd.Parameters.AddWithValue("@gst", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtGstin.Text) ? DBNull.Value : (object)txtGstin.Text;
                   cmd.Parameters.AddWithValue("@cloudApi", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtCloudApi.Text) ? DBNull.Value : (object)txtCloudApi.Text;
                   cmd.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtUsername.Text) ? DBNull.Value : (object)txtUsername.Text;
                   cmd.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtPassword.Password) ? DBNull.Value : (object)txtPassword.Password;
                   cmd.Parameters.AddWithValue("@selectusertype", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtcombox.Text) ? DBNull.Value : (object)txtcombox.Text;
                   cmd.Parameters.AddWithValue("@uploadlogo", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(id1) ? DBNull.Value : (object)id1;
                   //cmd.Parameters.AddWithValue("@uploadlogo", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(picUserimage.Source.ToString()) ? DBNull.Value : (object)picUserimage.Source.ToString();
                   cmd.Parameters.AddWithValue("@activationkey", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtActivationKey.Text) ? DBNull.Value : (object)txtActivationKey.Text;
                   cmd.Parameters.AddWithValue("@createdby", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(txtCreatedby.Text) ? DBNull.Value : (object)txtCreatedby.Text;
                   cmd.Parameters.AddWithValue("@date", SqlDbType.Date).Value = string.IsNullOrWhiteSpace(txtDate.Text) ? DBNull.Value : (object)txtDate.Text;
                   cmd.ExecuteNonQuery();
                   conn.Close();
                   MessageBox.Show("Successfully saved", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
                   refresh();
                   btnUpdate.IsEnabled = false;
                   New_Add_Profile nap = new New_Add_Profile();
                   nap.Show();
                   this.Close();
               }
           }
           catch (SqlException ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


What I have tried:

CREATE TABLE add_profile
( company_id int IDENTITY(1,1) PRIMARY KEY,
company_name varchar(100),
caption varchar(100),
address1 varchar(100),
address2 varchar(100),
mobileno varchar(100),
email varchar(100),
gst varchar(100),
cloudApi varchar(100),
username varchar(100),
password varchar(100),
selectusertype varchar(100),
uploadlogo varchar(100),
activationkey varchar(100),
createdby varchar(100),
date date
)	
Posted
Updated 15-Sep-22 23:06pm
Comments
Richard Deeming 16-Sep-22 3:40am    
There's a secret error somewhere in your secret stored procedure code. You need to fix that.

Seriously, nobody can help you fix a problem with your stored procedure when you haven't shown any part of your stored procedure code.

And using IDENTITY_INSERT in regular code is a sign that there's something seriously wrong with your design.
PIEBALDconsult 16-Sep-22 14:43pm    
Identity columns are evil, don't use them. Sequences are preferred. But however you do it, you should not be concerned with what the values are.

1 solution

Generally speaking, any system where you need to regularly set an IDENTITY value to a specific value is a bad design: it leads to some really nasty data integrity problems which can take a lot of human effort to work out.

Normally, people do this because they are preallocating ID's, and unless you are using GUIDs this is a very poor idea in a multiuser system as it is far too easy to get two users preallocating the same value - and then one "wins" and the other "loses". Depending on how well you code handles that loss, subsequent data can be entered against the wrong ID and allocated to the wrong client. At the very least, it annoys the heck out of your users when it does happen!

IDENTITY values are supposed to be unique, but not contiguous - if you need contiguous numbers use ROW_NUMBER (Transact-SQL) - SQL Server | Microsoft Docs[^] instead.
ID's should be allocated only when a row is inserted successfully, and not before: they aren't valid until that row is created so can't be used for anything else!
 
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