"invalid connection string" when updating datagridview

Connectivity
Enthusiast

"invalid connection string" when updating datagridview

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 Teradata.Client.Provider;

namespace Drop_Tables_App
{
    public partial class Form1 : Form
    {
        public DataTable dtRecord;
        public TdDataAdapter tdDA;
        public TdCommandBuilder tdcb;
        public TdConnectionStringBuilder conStringBuilder;
        public TdConnection tdCon;
        public Form1()
        {
            InitializeComponent();
        }

        private void Get_List_Click(object sender, EventArgs e)
        {
            conStringBuilder = new TdConnectionStringBuilder();
            conStringBuilder.DataSource = "";
            conStringBuilder.UserId = "";
            conStringBuilder.Password = "";
            conStringBuilder.ConnectionPooling = false;
            conStringBuilder.AuthenticationMechanism = "LDAP";
            //conStringBuilder.ConnectionTimeout = 1;
            DataTable dt;
            using (tdCon = new TdConnection(conStringBuilder.ConnectionString))
            {
                try
                {
                    tdCon.Open();
                    tdDA = new TdDataAdapter(@"SEL TRIM(DATABASENAME) as Databasename, TRIM(TABLENAME) as Tablename ,CASE WHEN CURR_IN=1 THEN 'Y' ELSE 'N' END AS TO_DELETE  FROM DROP_TABLE where CLOSE_IN=0 ORDER BY 1,2", tdCon);
                    dtRecord = new DataTable();
                    tdDA.Fill(dtRecord);
                    dataGridView1.DataSource = dtRecord;
                    tdcb = new TdCommandBuilder(tdDA);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void Update_Click(object sender, EventArgs e)
        {
                try
                {
                    //tdCon.Open();
                    
                    tdDA.Update(dtRecord);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            
        }
    }
}

Hi,

 

In my above code, i want to display some data to the user in datagridview and the user can update some data. After that i want to update the table with the data. when i run the tdda.update in update_click, i get error that "invalid connection string". Whereas the same connection string works fine in the upper section. Any idea how to solve this ? I am not an expert in .Net, but i am well verse with TD.

--samir

1 REPLY
Teradata Employee

Re: "invalid connection string" when updating datagridview

the "using" clause will Dispose the TdConnection. Therefore it cannot be used outside of the Get_List_Click. A solution is to initialize and open the connection outside of the context of Get_List_Click and Update_Click methods. Or alternatively initialize a new TdConnection with the ConnectionString and open the connection with a using clause.