locked
How to set SELECT parameter RRS feed

  • Question

  • I am trying to convert code from Visual Studio .NET 2003 to Visual Studio 2013.

    I am very new to VS2013.

    I had a ODBC connection and DataAdapter set up in VS2003. Now I have a DataSet with a TableAdapter set up in VS2013.

    my old code is:

     odbcDataAdapter4.SelectCommand.Parameters["Pkg"].Value = LnNbr;

     odbcdataAdapter4.Fill(dataSet1);

    In VS2013 I created DataSet1 with DataTable1 with DataTable1TableAdapter.

    I am unable to come up with the correct code to assign the LnNbr to the DataTable1TableAdapter parameter "Pkg".

    Thank You in Advance.

    Ron

    Wednesday, July 23, 2014 8:38 PM

Answers

  • I would avoid odbc if possible and use a client instead.  See code below.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string connStr = "Enter you connection string here";
                string SQL = "Select * From Table1 where Pkg = '@Pkg';";
    
                string LnNbr = "abc";
    
                SqlDataAdapter adapter = new SqlDataAdapter(SQL, connstr);
                adapter.SelectCommand.Parameters.Add("@Pkg", SqlDbType.VarChar);
    
                adapter.SelectCommand.Parameters["@Pkg"].Value = LnNbr;
    
                DataTable dt = new DataTable();
                adapter.Fill(dt);
    
                dataGridView1.DataSource = dt;
    
    
            }
        }
    }
    


    jdweng

    • Proposed as answer by Ioana Vasilescu Thursday, July 24, 2014 6:33 AM
    • Marked as answer by Caillen Wednesday, July 30, 2014 8:33 AM
    Thursday, July 24, 2014 1:09 AM