none
Error in code RRS feed

  • Question

  • When clicking on "Create Account"(btnCreateAccount) button, I receive error System.ArgumentExceptionj: 'Keyword not supported: N10468000115\sqlhunter;initial catalog'.'

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace SimpleDataApp
    {
        public partial class NewCustomer : Form
        {
            private int parsedCustomerID;
            private int orderID;
            private bool IsCustomerNameValid()
            {
                if (txtCustomerName.Text == "")
                {
                    MessageBox.Show("Please enter a name.");
                    return false;
                }
                else
                {
                    return true;
                }
            }
            private bool IsOrderDataValid()
            {
                // Verify that CustomerID is present.
                if (txtCustomerID.Text == "")
                {
                    MessageBox.Show("Please create customer account before placing order.");
                    return false;
                }
                // Verify that Amount isn't 0.
                else if ((numOrderAmount.Value < 1))
                {
                    MessageBox.Show("Please specify an order amount.");
                    return false;
                }
                else
                {
                    // Order can be submitted.
                    return true;
                }
            }
    
            /// <summary>
            /// Clears the form data.
            /// </summary>
            private void ClearForm()
            {
                txtCustomerName.Clear();
                txtCustomerID.Clear();
                dtpOrderDate.Value = DateTime.Now;
                numOrderAmount.Value = 0;
                this.parsedCustomerID = 0;
            }
            public NewCustomer()
            {
                InitializeComponent();
            }
    
            private void NewCustomer_Load(object sender, EventArgs e)
            {
    
            }
    
            private void btnCreateAccount_Click(object sender, EventArgs e)
            {
                if (IsCustomerNameValid())
                {               // Create the connection.
                    using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connString))
                    {
                        // Create a SqlCommand, and identify it as a stored procedure.
                        using (SqlCommand sqlCommand = new SqlCommand("Sales.uspNewCustomer", connection))
                        {
                            sqlCommand.CommandType = CommandType.StoredProcedure;
    
                            // Add input parameter for the stored procedure and specify what to use as its value.
                            sqlCommand.Parameters.Add(new SqlParameter("@CustomerName", SqlDbType.NVarChar, 40));
                            sqlCommand.Parameters["@CustomerName"].Value = txtCustomerName.Text;
    
                            // Add the output parameter.
                            sqlCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
                            sqlCommand.Parameters["@CustomerID"].Direction = ParameterDirection.Output;
    
                            try
                            {
                                connection.Open();
    
                                // Run the stored procedure.
                                sqlCommand.ExecuteNonQuery();
    
                                // Customer ID is an IDENTITY value from the database.
                                this.parsedCustomerID = (int)sqlCommand.Parameters["@CustomerID"].Value;
    
                                // Put the Customer ID value into the read-only text box.
                                this.txtCustomerID.Text = Convert.ToString(parsedCustomerID);
                            }
                            catch
                            {
                                MessageBox.Show("Customer ID was not returned. Account could not be created.");
                            }
                            finally
                            {
                                connection.Close();
                            }
                        }
                    }
                }
            }
    
            private void btnPlaceOrder_Click(object sender, EventArgs e)
            {
                if (IsOrderDataValid())
                {
                    // Create the connection.
                    using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connString))
                    {
                        // Create SqlCommand and identify it as a stored procedure.
                        using (SqlCommand sqlCommand = new SqlCommand("Sales.uspPlaceNewOrder", connection))
                        {
                            sqlCommand.CommandType = CommandType.StoredProcedure;
    
                            // Add the @CustomerID input parameter, which was obtained from uspNewCustomer.
                            sqlCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
                            sqlCommand.Parameters["@CustomerID"].Value = this.parsedCustomerID;
    
                            // Add the @OrderDate input parameter.
                            sqlCommand.Parameters.Add(new SqlParameter("@OrderDate", SqlDbType.DateTime, 8));
                            sqlCommand.Parameters["@OrderDate"].Value = dtpOrderDate.Value;
    
                            // Add the @Amount order amount input parameter.
                            sqlCommand.Parameters.Add(new SqlParameter("@Amount", SqlDbType.Int));
                            sqlCommand.Parameters["@Amount"].Value = numOrderAmount.Value;
    
                            // Add the @Status order status input parameter.
                            // For a new order, the status is always O (open).
                            sqlCommand.Parameters.Add(new SqlParameter("@Status", SqlDbType.Char, 1));
                            sqlCommand.Parameters["@Status"].Value = "O";
    
                            // Add the return value for the stored procedure, which is  the order ID.
                            sqlCommand.Parameters.Add(new SqlParameter("@RC", SqlDbType.Int));
                            sqlCommand.Parameters["@RC"].Direction = ParameterDirection.ReturnValue;
    
                            try
                            {
                                //Open connection.
                                connection.Open();
    
                                // Run the stored procedure.
                                sqlCommand.ExecuteNonQuery();
    
                                // Display the order number.
                                this.orderID = (int)sqlCommand.Parameters["@RC"].Value;
                                MessageBox.Show("Order number " + this.orderID + " has been submitted.");
                            }
                            catch
                            {
                                MessageBox.Show("Order could not be placed.");
                            }
                            finally
                            {
                                connection.Close();
                            }
                        }
                    }
                }
            }
    
            private void btnAddAnotherAccount_Click(object sender, EventArgs e)
            {
                this.ClearForm();
            }
    
            private void btnAddFinish_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
    

    Thursday, November 14, 2019 3:40 PM

Answers

  • My connection string N10468000115\SQLHUNTER;Initial Catalog=Sales;Integrated Security=True;

    If that is all of your connection string, then there is indeed something missing. You need to add a keyword at the beginning to indicate that the first part is the server name. There are various synonyms for that; one way to do it is this:

    Server=N10468000115\SQLHUNTER;Initial Catalog=Sales;Integrated Security=True;

     
    • Marked as answer by Decompressor Friday, November 15, 2019 8:57 AM
    Friday, November 15, 2019 7:31 AM
    Moderator

All replies

  • Look at this part in the error:

    N10468000115\sqlhunter;Initial Catalog

    It looks like a mistake in a connection string. Unfortunately, we cannot examine the connection string to see what is wrong with it, because the program is taking it from Properties.Settings, so it is not visible in the fragment of code that you posted. But there must be something wrong within the string given the message complaining about "keyword not supported".

    Thursday, November 14, 2019 8:20 PM
    Moderator
  • Looks like you didn't specify the initial catalog

    N10468000115\sqlhunter;initial catalog'.'

    Should look like

    private string ConnectionString = "Data Source=N10468000115\\sqlhunter;Initial Catalog=SomeDatabase;Integrated Security=True";


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 14, 2019 8:24 PM
    Moderator
  • Hi pavlob, 

    Thank you for posting here.

    According to your question, I have a question to confirm with you.

    Could your provide more information about ‘StoredProcedure’ and your design of database?

    Please provide more details about your exception with which line of the code thrown the exception, and it will help us to analyze your problem.

    Besides, if your connect String is incorrect, you can refer to the following reference to get the correct connection String from your database.

    How to get the connection String from a database

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 15, 2019 5:31 AM
    Moderator
  • I made project from https://docs.microsoft.com/en-us/visualstudio/data-tools/create-a-simple-data-application-by-using-adonet?view=vs-2019

    My connection string N10468000115\SQLHUNTER;Initial Catalog=Sales;Integrated Security=True;

    I saved it in "Value" column Settings.settings file of my project 

    Friday, November 15, 2019 6:35 AM
  • My connection string N10468000115\SQLHUNTER;Initial Catalog=Sales;Integrated Security=True;

    If that is all of your connection string, then there is indeed something missing. You need to add a keyword at the beginning to indicate that the first part is the server name. There are various synonyms for that; one way to do it is this:

    Server=N10468000115\SQLHUNTER;Initial Catalog=Sales;Integrated Security=True;

     
    • Marked as answer by Decompressor Friday, November 15, 2019 8:57 AM
    Friday, November 15, 2019 7:31 AM
    Moderator