I am trying to connect Teradata using .Net with the below code.
But when I execute it, it throws an error stating Invalid connection string
on
TdDataAdapter adapter = new TdDataAdapter(cn.ConnectionString,cmd.CommandText);
Here's the complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Teradata.Client.Provider;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TdConnectionStringBuilder connectionStringBuilder = new TdConnectionStringBuilder();
connectionStringBuilder.DataSource = "URK";
connectionStringBuilder.Database = "DB";
connectionStringBuilder.UserId = "USERNAME";
connectionStringBuilder.Password = "PASSWORD";
connectionStringBuilder.AuthenticationMechanism = "LDAP";
TdConnection cn = new TdConnection();
cn.ConnectionString = connectionStringBuilder.ConnectionString;
cn.Open();
TdCommand cmd = new TdCommand("EXEC MACRONAME", cn);
TdDataReader reader = cmd.ExecuteReader();
TdDataAdapter adapter = new TdDataAdapter(cn.ConnectionString,cmd.CommandText);
DataSet ds = new DataSet();
adapter.Fill(ds);
myLabel.Text= ds.Tables[0].Rows[0]["event_id"].ToString();
cmd.Dispose();
cn.Close();
}
}
I tried using connectionStringBuilder.ConnectionString instead of the one used above but I still got the same error.
Just swap parameters
TdDataAdapter adapter = new TdDataAdapter(cmd.CommandText, cn.ConnectionString);
according to signature of TdDataAdapter constructor
public TdDataAdapter(
string commandText,
string connectionString
)
See docs
Related
Hey I'm trying to add database info into my webpage. This is the code I am trying.
using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
namespace DatabaseAddDemo
{
public partial class Content : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["key"] != null)
{
try
{
SqlConnection sqlConn = new SqlConnection(#"Data Source=officedev1;Initial Catalog=TestDatabase;User ID=sa;Password=Password11;pooling='true';Connect Timeout=3000; Max Pool Size=200;MultipleActiveResultSets='true'");
SqlCommand cmdPreWork = new SqlCommand(#"select * from CKEditor_Table where #ID = #key", sqlConn);
string key = Request.QueryString["keys"].ToString();
contentLiteral.Text = key;
cmdPreWork.Parameters.Add("#Information", SqlDbType.Char).Value = key;
Console.WriteLine(contentLiteral);
SqlDataAdapter daPreWork = new SqlDataAdapter(cmdPreWork);
DataTable dtPreWork = new DataTable();
daPreWork.Fill(dtPreWork);
Grid.DataSource = dtPreWork;
Grid.DataBind();
}
catch (Exception ex)
{
lblError.Text = "Could not open connection";
}
}
}
}
}
Whenever I try to display the information, I get the lblError text, telling me I could not open the connection. I'm not sure what to do. Please help.
You should be able to DEBUG this as stated in the comments. But in your code your select query expects a [Key] parameter and you are passing a [Information] parameter.
Change it as below:
SqlCommand cmdPreWork = new SqlCommand(#"select * from CKEditor_Table
where #ID = #key", sqlConn);
cmdPreWork.Parameters.Add("#key", SqlDbType.Char).Value = key;
i'm trying to retrieve a picture saved in the database in BLOB format
but it gives an Exception that the parameter of MemoryStream is invalid in this Line
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
and this is the full code :
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 MySql.Data.MySqlClient;
using System.IO;
namespace InsertImage2
{
public partial class Form1 : Form
{
MySqlConnection myConn = new MySqlConnection("datasource=localhost;port=****;username=****;password=****;");
MySqlCommand cmd;
MySqlDataAdapter da;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string query = "select * from images_database.images where id = '"+textBox1.Text+"';";
cmd = new MySqlCommand(query, myConn);
da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
byte[] img = (byte[])dt.Rows[0][1];
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
da.Dispose();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class RepeaterEx2 : System.Web.UI.Page {
SqlConnection cn = null;
SqlDataAdapter da = null;
DataSet ds = null;
String strSqlQuery = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
cn = new SqlConnection();
cn.ConnectionString = "Server=(local);Data base=TestDb;Uid=sa;Password=123";
if (!Page.IsPostBack)
{
}
}
void BindEmpData
{
SqlDataAdapter da=new SqlDataAdapter( "select e.ENO,e.ENAME,e.JOB,e.SAL,d.DNAME form EMPLOYEE e,DEPARTMENT d where e.DNO=d.DNO",cn);
da.Fill(ds,"EMPLOYEE");//here showing set or get accessorexpected error at "da"
Repeater1.DataSource=ds.Table["EMPLOYEE"];
Repeater1.DataBind();
}
}
I'm getting this error:
A get or set accessor expected
How do I resolve this error?
You need parentheses after the function name here:
void BindEmpData()
{
...
}
Also, you'll want to make sure you initialize the DataSet correctly:
void BindEmpData()
{
SqlDataAdapter da = new SqlDataAdapter("select e.ENO,e.ENAME,e.JOB,e.SAL,d.DNAME form EMPLOYEE e,DEPARTMENT d where e.DNO=d.DNO",cn);
DataSet ds = new DataSet();
da.Fill(ds,"EMPLOYEE");
Repeater1.DataSource = ds.Table["EMPLOYEE"];
Repeater1.DataBind();
}
And at this point you can remove the ds and da class members, since they are no longer being used (they've been replaced by function variables).
Parentheses is required to differentiate a method from a property that requires the get/set syntax
SO here it goes. I have a model of dataset that works just fine and it imports data from database and gives it to crystal report. this solution works but it is very time consuming I was wondering if there is any other way of doing this...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connetionString = null;
OracleConnection connection;
OracleDataAdapter OracleAdapter;
DataSet ds = new DataSet();
string firstSql = null;
connetionString = "datasoruce";
connection = new OracleConnection(connetionString);
string secondSql = "select statementnumber from erocks.statement_data_domestic";
connection.Open();
//OracleAdapter = new OracleDataAdapter(firstSql, connection);
//OracleAdapter.Fill(ds, "domestic");
OracleAdapter = new OracleDataAdapter(secondSql, connection);
OracleAdapter.Fill(ds, "statement");
connection.Close();
ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(#"c:\users\desktop\statement.rpt");
DataTable stmt = ds.Tables["statement"];
string stmtnumber="";
for (int i = 0; i < stmt.Rows.Count - 1; i++)
{
stmtnumber = stmt.Rows[i][0].ToString();
firstSql = #"SELECT DISTINCT statement_header.statementnumber,
statement_details.invoicedate,
statement_details.invoicenumber,
statement_details.invoicetotal,
statement_details.doc_type,
statement_header.statementtotal,
statement_details.bunumber_ru,
statement_details.bunumber,
statement_details.description,
statement_details.reference_number,
statement_header.remto_zip,
statement_header.remto_city,
statement_header.remto_state,
statement_header.remto_mailname,
statement_header.remto_addr1,
statement_header.remto_addr2,
statement_header.remto_addr3,
statement_header.soldto_city,
statement_header.soldto_state,
statement_header.soldto_zip,
statement_header.soldto_addr1,
statement_header.soldto_addr2,
statement_header.soldto_addr3,
statement_header.balance_forward,
statement_header.statementdate,
statement_header.custid,
statement_header.custname,
statement_header.phone_prefix,
statement_header.phone_number,
statement_details.purchases,
statement_details.payments,
statement_details.misc_credit2,
statement_details.misc_credit1,
statement_header.company_number,
statement_header.statementpurchases,
statement_header.statementpayments,
statement_header.statementmisc_credit1,
statement_header.statementmisc_credit2,
statement_header.nomailnoprint,
statement_header.SOLDTOCOUNTRYCODE,
statement_header.SOLDTOCOUNTRYNAME,
statement_header.CREDITZEROFLAG
FROM STATEMENT_DATA_DOMESTIC statement_header
INNER JOIN STATEMENT_DATA_DETAILS statement_details
ON statement_header.statementnumber =
statement_details.statementnumber
where statement_header.statementnumber="+stmtnumber;
connection.Open();
OracleAdapter = new OracleDataAdapter(firstSql, connection);
OracleAdapter.Fill(ds, "domestic");
OracleAdapter.Dispose();
connection.Close();
reportDoc.SetDataSource(ds.Tables["domestic"]);
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = #"d:\pdf\"+ stmtnumber + ".pdf";
CrExportOptions = reportDoc.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
reportDoc.Export();
ds.Tables["domestic"].Clear();
}
}
}
}
It will be faster if you retrieve the data for all statements, group it by statementID inside the report and burst the report by this group. Bursting will generate a separate file for each group. In such way you will be able to generate all the files with one call to the database.
I use C#.NET (WebApp) VS 2008 SP1
I want click next page from crystal report v.10.5.3700 (Framework 3.5) but it's show input data to next page
The report you requested requires further information
Server name:
Database name:
User name:
Password:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Transactions;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using CrystalDecisions.Web;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
namespace testReport
{
public partial class _cldClass : System.Web.UI.Page
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
SqlConnection Conn;
DataSet ds = new DataSet();
DataTable dt = null;
string strConnString = WebConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
string strSQL = null;
public string userDB
{
get { return WebConfigurationManager.AppSettings["userDB"]; }
}
public string pwdDB
{
get { return WebConfigurationManager.AppSettings["pwdDB"]; }
}
public string srvDB
{
get { return WebConfigurationManager.AppSettings["srvDB"]; }
}
public string dbName
{
get { return WebConfigurationManager.AppSettings["dbName"]; }
}
//protected void Page_Load(object sender, EventArgs e)
//{
//}
protected void Page_Init(object sender, EventArgs e)
{
TextBox2.Text = DateTime.Now.ToString("yyyy-MM-dd", new CultureInfo("en-US"));
Conn = new SqlConnection(strConnString);
Conn.Open();
if (Conn.State == ConnectionState.Open)
{
this.Label1.Text = "Connected";
}
else
{
this.Label1.Text = "Connect Failed";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
strSQL = "SELECT * FROM fTime WHERE fDate='" + TextBox2.Text + "'";
objConn.ConnectionString = strConnString;
var _with1 = objCmd;
_with1.Connection = objConn;
_with1.CommandText = strSQL;
_with1.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds, "cReport");
dt = ds.Tables[0];
dtAdapter = null;
objConn.Close();
objConn = null;
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("Report\\CrystalReport.rpt"));
rpt.SetDataSource(dt);
rpt.SetDatabaseLogon(userDB, pwdDB, srvDB, dbName);
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.RefreshReport();
}
}
}
Thanks for your time :)
Normally you need to enter these informations when you created the report document directly from a database, that means when you used the report wizard and selected a table from the database. The report document itself contains the connection information to that data source but don't saves the logon information.
To overcome this you may create a typed dataset first from your database table you like to use in the report. Then create a report document and use the wizard to set the dataset (not the database table directly) as data source for your report. In your code you may then fill the dataset and just pass it to the report.
I ran into this today, and the reason I got the error was because I didn't have the correct provider installed on my server.
What I did was this:
CrystalReportViewer1.EnableDatabaseLogonPrompt = false;
That gave me a new error: Logon failed. Details: ADO Error Code: 0x Source: ADODB.Connection Description: Provider cannot be found. It may not be properly installed
After that, I checked the OLE DB provider in the report. Installing the SQL Native Client tools fixed the problem.