How to set this Connection String?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi expert, I need help here. Problem is connection string return null and no data load into datatable. Here is the code

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas; color:blue public<span style="font-size:9.5pt; font-family:Consolas
<span style="color:blue partial <span style="color:blue class <span style="color:#2B91AF
Form1 : <span style="color:#2B91AF Form
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{<span>
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#2B91AF OpenFileDialog dialog = <span style="color:blue
new <span style="color:#2B91AF OpenFileDialog();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#2B91AF DialogResult dr;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue public Form1()
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
InitializeComponent();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue private <span style="color:blue void button1_Click(<span style="color:blue object sender,
<span style="color:#2B91AF EventArgs e)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dialog.Filter = <span style="color:#A31515 "Access 2007 |*.accdb";
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dialog.Title = <span style="color:#A31515 "Please select a database";
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dialog.FilterIndex = 1;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dr = dialog.ShowDialog();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue if (dr == <span style="color:#2B91AF DialogResult.OK)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
textBox1.Text = dialog.FileName;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue string dbConn = <span style="color:#A31515
"Provider=Microsoft.ACE.OLEDB.12.0;" +
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#A31515 "Data Source=" + textBox1.Text.ToString() +
<span style="color:#A31515 ";" +
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#A31515 "Jet OLEDB:Database Password=123456;Persist Security Info=False";
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#2B91AF DataTable dtLoad = <span style="color:blue
new <span style="color:#2B91AF DataTable();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#2B91AF DataRow strDatarow = <span style="color:blue
null;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dtLoad.Columns.Add(<span style="color:#A31515 "First Name");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dtLoad.Columns.Add(<span style="color:#A31515 "Last Name");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dtLoad.Columns.Add(<span style="color:#A31515 "Address");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dtLoad.Columns.Add(<span style="color:#A31515 "Contact");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dtLoad.Columns.Add(<span style="color:#A31515 "Reg Date");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue string strGenReport = <span style="color:#A31515
"SELECT fname,lname,address,contact,regdate FROM tcustomer";
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue using (<span style="color:#2B91AF OleDbConnection conString =
<span style="color:blue new <span style="color:#2B91AF OleDbConnection(dbConn))
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue using (<span style="color:#2B91AF OleDbCommand cmdGenReport =
<span style="color:blue new <span style="color:#2B91AF OleDbCommand(strGenReport, conString))
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span> {
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue try
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
conString.Open();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:#2B91AF OleDbDataReader readGenReport;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
readGenReport = cmdGenReport.ExecuteReader();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue while (readGenReport.Read())
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
strDatarow[<span style="color:#A31515 "First Name"] = readGenReport[<span style="color:#A31515 "fname"].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
strDatarow[<span style="color:#A31515 "Last Name"] = readGenReport[<span style="color:#A31515 "lname"].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
strDatarow[<span style="color:#A31515 "Address"] = readGenReport[<span style="color:#A31515 "address"].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
strDatarow[<span style="color:#A31515 "Contact"] = readGenReport[<span style="color:#A31515 "contact"].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
strDatarow[<span style="color:#A31515 "Reg Date"] = readGenReport[<span style="color:#A31515 "regdate "].ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span> dtLoad.Rows.Add(strDatarow);
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
readGenReport.Close();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue catch (<span style="color:#2B91AF Exception ex)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
ex.ToString();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
dataGridView1.DataSource = dtLoad;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span>
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}<span>

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas }
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
Thanks in advance...<span style="font-size:9.5pt; font-family:Consolas
<br/>

View the full article
 
Back
Top