Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void btnSave_Click(object sender, RoutedEventArgs e)
     {
         if (txtCompanyId.Text == "")
         {
             MessageBox.Show("Please Enter CompanyId", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtCompanyId.Focus();
         }
         else if (txtCompanyName.Text == "")
         {
             MessageBox.Show("Please fill CompanyName", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtCompanyName.Focus();
         }
         else if (txtCaption.Text == "")
         {
             MessageBox.Show("Please fill Caption", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtCaption.Focus();
         }
         else if (txtAddress1.Text == "")
         {
             MessageBox.Show("Please fill Address1", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtAddress1.Focus();
         }
         else if (txtAddress2.Text == "")
         {
             MessageBox.Show("Please fill Address2", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtAddress2.Focus();
         }
        else if(txtMobile.Text == "")
         {
             MessageBox.Show("Please fill MobileNo", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtMobile.Focus();
         }
         else if(txtEmail.Text == "")
         {
             MessageBox.Show("Please fill Email", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtEmail.Focus();
         }
         else if(txtGstin.Text == "")
         {
             MessageBox.Show("Please fill Gstin", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtGstin.Focus();
         }
         else if(txtCloudApi.Text == "")
         {
             MessageBox.Show("Please fill CloudApi", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtCloudApi.Focus();
         }
        else if(txtUsername.Text == "")
         {
             MessageBox.Show("Please fill Username", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtUsername.Focus();
         }
         else if(txtPassword.ToString() == "")
         {
             MessageBox.Show("Please fill Password", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtPassword.Focus();
         }
         else if(txtActivationKey.Text == "")
         {
             MessageBox.Show("Please fill ActivationKey", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             txtActivationKey.Focus();
         }
         else
         {
             try
             {
                 SqlConnection conn = new SqlConnection(ConnectionString);
                 conn.Open();
                 SqlCommand cmd = new SqlCommand("add_profile_details", conn);
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.AddWithValue("@company_id", SqlDbType.VarChar).Value = txtCompanyId;
                 cmd.Parameters.AddWithValue("@company_name", SqlDbType.VarChar).Value = txtCompanyName;
                 cmd.Parameters.AddWithValue("@caption", SqlDbType.VarChar).Value = txtCaption;
                 cmd.Parameters.AddWithValue("@address1", SqlDbType.VarChar).Value = txtAddress1;
                 cmd.Parameters.AddWithValue("@address2", SqlDbType.VarChar).Value = txtAddress2;
                 cmd.Parameters.AddWithValue("@mobileno", SqlDbType.VarChar).Value = txtMobile;
                 cmd.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = txtEmail;
                 cmd.Parameters.AddWithValue("@gstn", SqlDbType.VarChar).Value = txtGstin;
                 cmd.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = txtUsername;
                 cmd.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = txtPassword;
                 cmd.Parameters.AddWithValue("@selectusertype", SqlDbType.VarChar).Value = txtcombox;
                 cmd.Parameters.AddWithValue("@uploadlogo", SqlDbType.VarChar).Value = picUserimage;
                 cmd.Parameters.AddWithValue("@activationkey", SqlDbType.VarChar).Value = txtActivationKey;
                 //conn.Open();
                 cmd.ExecuteNonQuery();
                 conn.Close();
                 MessageBox.Show("Successfully saved", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
             }
         }

     }


What I have tried:

try
{
    SqlConnection conn = new SqlConnection(ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("add_profile_details", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@company_id", SqlDbType.VarChar).Value = txtCompanyId;
    cmd.Parameters.AddWithValue("@company_name", SqlDbType.VarChar).Value = txtCompanyName;
    cmd.Parameters.AddWithValue("@caption", SqlDbType.VarChar).Value = txtCaption;
    cmd.Parameters.AddWithValue("@address1", SqlDbType.VarChar).Value = txtAddress1;
    cmd.Parameters.AddWithValue("@address2", SqlDbType.VarChar).Value = txtAddress2;
    cmd.Parameters.AddWithValue("@mobileno", SqlDbType.VarChar).Value = txtMobile;
    cmd.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = txtEmail;
    cmd.Parameters.AddWithValue("@gstn", SqlDbType.VarChar).Value = txtGstin;
    cmd.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = txtUsername;
    cmd.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = txtPassword;
    cmd.Parameters.AddWithValue("@selectusertype", SqlDbType.VarChar).Value = txtcombox;
    cmd.Parameters.AddWithValue("@uploadlogo", SqlDbType.VarChar).Value = picUserimage;
    cmd.Parameters.AddWithValue("@activationkey", SqlDbType.VarChar).Value = txtActivationKey;
    //conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    MessageBox.Show("Successfully saved", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
}
Posted
Updated 27-Aug-22 0:24am
Comments
Graeme_Grant 27-Aug-22 6:20am    
When posing questions, you need to explain the issue, the EXACT error message (copy & paste) and the line that the error occurs on.

You are setting all the database fields to contain the windows control id and not the text content of the controls.

cmd.Parameters.AddWithValue("@company_id", SqlDbType.VarChar).Value = txtCompanyId;

and
cmd.Parameters.AddWithValue("@company_id", SqlDbType.VarChar).Value = txtCompanyId.Text;

You might also want to improve your field validation as fields with spaces would be considered valid.

I would suggest

if (txtCompanyId.Text.Trim() == "")

or
if (string.IsNullOrEmpty(txtCompanyId.Text.Trim()))
{
    .....
}
 
Share this answer
 
v2
Comments
prashanth manoj 27-Aug-22 6:26am    
did not work
Graeme_Grant 27-Aug-22 6:39am    
You need to fix all, not just the one that we listed as our example.
Graeme_Grant 27-Aug-22 6:31am    
:P
prashanth manoj 27-Aug-22 6:56am    
again show this...
No mapping exists from object type System.Windows.Controls.TextBox to a known managed provider native type.'
again show this
prashanth manoj 27-Aug-22 6:56am    
in executenonquery();
Yep, you are trying to pass a control object to a string field. That is what the error message is saying.

You need to use the Text property from the control is get the value. Here is one example:

Change:
C#
cmd.Parameters.AddWithValue("@company_id", SqlDbType.VarChar).Value = txtCompanyId;

To:
<pre lang="C#">
cmd.Parameters.AddWithValue("@company_id", SqlDbType.VarChar).Value = txtCompanyId.Text;
 
Share this answer
 
Comments
prashanth manoj 27-Aug-22 6:25am    
did not work
Graeme_Grant 27-Aug-22 6:33am    
You have incorrectly mapped type for all, not just the one example that I gave.
Tony Hill 27-Aug-22 6:25am    
Damn, beat me to it.
prashanth manoj 27-Aug-22 6:28am    
'No mapping exists from object type System.Windows.Controls.TextBox to a known managed provider native type.'
again show this
Graeme_Grant 27-Aug-22 6:38am    
You need to fix all, not just the one that we listed as our example.

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