Search code examples
c#sql-serverwpfdatetimesqldatatypes

Issues with INSERT statement (I think)


I'm working on a quote manager for my boss at work and I'm having some issues. This is a WPF C# application and it's the first time I've ever built anything that works with a SQL Server database. I'm currently having three issues.

Background:

When the user opens the application they're greeted with a DataGrid, a new quote button and several other controls that I haven't yet created. When they press the new quote button a new window pops up with a form that will have text boxes for things like Customer Name, quantity, etc. At the bottom of that form is a submit button, at which point the window will close and the information they added will be inserted into the DataGrid as a new row.

Problem One:

One of the fields in my database is called Open_Quote and it is supposed to be hold the date we received the order. This is handled programmatically and is what my first question involves. I'll include all of the code at the bottom of this post, but when the user hits submit I receive the following error: "Conversion failed when converting date and/or time from character string."

Problem Two:

In an attempt to test the rest of my code and go back later to fix the date issue, I commented that code out and tried running my program again. This time I get a different error: "Incorrect syntax around 'newQuote.Qty'."

Problem Three:

Again, commenting that code out in order to finally test the rest of my code, I received a third error: "string or binary data would be truncated. This process has been terminated."

My hope is that there's one piece of code that's causing all three of these issues, but I could be completely off there. I've been pulling my hair out for over a day trying to figure this out. Anyway, here's the code:

newQuote.xaml.cs:

    private void SubmitQuotebtn_Click(object sender, RoutedEventArgs e)
    {
        CustomerData newQuote = new CustomerData();

        int quantity;
        quantity = Convert.ToInt32(Qtytxt.Text);

        string theDate = System.DateTime.Today.Date.ToString("d");

        newQuote.OpenQuote = theDate;
        newQuote.CustomerName = CustNametxt.Text;
        newQuote.OEMName = OemNametxt.Text;
        newQuote.Qty = quantity;
        newQuote.QuoteNumber = QuoteNumtxt.Text;
        newQuote.FdNumber = FabDrawingNumtxt.Text;
        newQuote.RfqNumber = RfqNumtxt.Text;
        newQuote.RevNumber = RevNumtxt.Text;

        try
        {
            string insertConString = Sqtm.Properties.Settings.Default.SqtmDbConnectionString;

            using (SqlConnection insertConnection = new SqlConnection(insertConString))
            {
                insertConnection.Open();

                SqlCommand cmd = new SqlCommand("INSERT INTO General_Info(Open_Quote, Customer_Name, OEM_Name, Qty, Quote_Num, Fab_Drawing_Num, "
                                                + "Rfq_Num, Rev_Num) values('newQuote.OpenQuote', 'newQuote.CustomerName', 'newQuote.OemName', 'newQuote.Qty' "
                                                + "'newQuote.QuoteNumber', 'newQuote.FdNumber', 'newQuote.RfqNumber', 'newQuote.RevNumber')", insertConnection);

                cmd.ExecuteNonQuery();

            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

CustomerData.cs:

    class CustomerData
{
    private string _CustomerName;
    private string _OEMName;
    private string _OpenQuote;
    private int _Qty;
    private string _QuoteNumber;
    private string _FdNumber;
    private string _RfqNumber;
    private string _RevNumber;

    public CustomerData()
    {
        // empty constructor
    }

    public string CustomerName
    {
        get { return _CustomerName; }
        set { _CustomerName = value; }
    }

    public string OpenQuote
    {
        get { return _OpenQuote; }
        set { _OpenQuote = value; }
    }

    public string OEMName
    {
        get { return _OEMName; }
        set { _OEMName = value; }
    }

    public int Qty
    {
        get { return _Qty; }
        set { _Qty = value; }
    }

    public string QuoteNumber
    {
        get { return _QuoteNumber; }
        set { _QuoteNumber = value; }
    }

    public string FdNumber
    {
        get { return _FdNumber; }
        set { _FdNumber = value; }
    }

    public string RfqNumber
    {
        get { return _RfqNumber; }
        set { _RfqNumber = value; }
    }

    public string RevNumber
    {
        get { return _RevNumber; }
        set { _RevNumber = value; }
    }
}

And as a reference, here's how I set up this table in SQLServer:

Open_Quote, date, not null
Customer_Name, varchar(25), not null
OEM_Name, varchar(25), null
Qty, int, not null
Qute_Num, varchar(20), null
Fab_Drawing_Num, varchar(20), not null
Rfq_Num, varchar(10), null
Rev_Num, varchar(10), null

Thanks in advance to anyone who helps me out,

  • Andrew

Solution

  • Try again with parameters and let us know how it goes. http://www.dotnetperls.com/sqlparameter

    Edit: Or what Tieson T. said. That'd be even better just a little more involved.

    To get the data to show in the datagrid, after you do the insert, you can rebind the grid. Make sure your update the datasource so it repulls the data you just inserted. If you have problems, show where/how you are setting the datasource for the grid.