The ConnectionString property has not been initialized.

Posted by Sudheep.grandhe under C# on 12/19/2011 | Points: 10 | Views : 8008 | Status : [Member] | Replies : 8
Hi,

autocomplete textbox in windows applications?.here is my code.

public partial class AuthenticationForm : Form
{
public string strConnection = ConfigurationManager.AppSettings["data source=mars-104;initial catalog=Sudheeptest;integrated security=true"];
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
private void LoginForm_Load(object sender, EventArgs e)
{
SqlDataReader dReader;
SqlConnection conn = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
conn.ConnectionString = strConnection;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct[UserName] from [tbl_login] order by [UserName] asc";
if (conn.State == ConnectionState.Closed)
{
conn.Open();//error shows here The ConnectionString property has not been initialized.
}
dReader = cmd.ExecuteReader();
if (dReader.HasRows == true)
{
while (dReader.Read())
namesCollection.Add(dReader["UserName"].ToString());
}
else
{
MessageBox.Show("Data Not Found");
}
dReader.Close();
txt_username.AutoCompleteMode = AutoCompleteMode.Suggest;
txt_username.AutoCompleteSource = AutoCompleteSource.CustomSource;
txt_username.AutoCompleteCustomSource = namesCollection;
txt_username.Focus();
}
}

Best,
Sudheep.



Responses

Posted by: Vuyiswamb on: 12/19/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
try this


public partial class AuthenticationForm : Form 

{
public string strConnection = ConfigurationManager.AppSettings["data source=mars-104;initial catalog=Sudheeptest;integrated security=true"];
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
private void LoginForm_Load(object sender, EventArgs e)
{
SqlDataReader dReader;
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct[UserName] from [tbl_login] order by [UserName] asc";
if (conn.State == ConnectionState.Closed)
{
conn.Open();//error shows here The ConnectionString property has not been initialized.
}
dReader = cmd.ExecuteReader();
if (dReader.HasRows == true)
{
while (dReader.Read())
namesCollection.Add(dReader["UserName"].ToString());
}
else
{
MessageBox.Show("Data Not Found");
}
dReader.Close();
txt_username.AutoCompleteMode = AutoCompleteMode.Suggest;
txt_username.AutoCompleteSource = AutoCompleteSource.CustomSource;
txt_username.AutoCompleteCustomSource = namesCollection;
txt_username.Focus();
}
}





Thank you for posting at Dotnetfunda
[Administrator]

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

it shows the same error.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 12/29/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,
Check the following information:
1 ) set username,password for your database
http://www.codeproject.com/KB/cs/Three_Layer_Architecture.aspx


Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Priyagoel_24 on: 7/16/2013 [Member] Starter | Points: 25

Up
0
Down
protected void btnLogIn_Click(object sender, EventArgs e)
{
string strSelect = "SELECT COUNT(*) FROM login1 WHERE UserName = @Username AND Password = @Password";
SqlConnection cnn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSelect;
SqlParameter username = new SqlParameter("@Username", SqlDbType.VarChar, 50);
username.Value = txtUserName.Text.Trim().ToString();
cmd.Parameters.Add(username);
SqlParameter password = new SqlParameter("@Password", SqlDbType.VarChar, 50);
password.Value = txtPwd.Text.Trim().ToString();
cmd.Parameters.Add(password);
cnn.Open();
int result = (Int32)cmd.ExecuteScalar();
cnn.Close();
if (result >= 1)
{
Session["userName"] = txtUserName.Text.ToString().Trim();
Response.Redirect("UpdateProfile.aspx");
}
else
lblMsg.Text = "Incorrect Username or Password";


}
}

error coming .


Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hydtechie on: 7/22/2013 [Member] Starter | Points: 25

Up
0
Down
guys...Please stop speculating on this like MVPs.....,

I could reproduce with a strong point, If there is an exception occurred, if you have rolled back your transaction, and try to continue the further data retrieval, Connection is Closed, so the error.

if you want more details about your bug, In VS IDE, keep exception ON. ( Debug --> Exceptions, check on exceptions... )you would see the dirty code which causing this error !

Hyd
tec hie

HydTechie

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Priyagoel_24 on: 8/1/2013 [Member] Starter | Points: 25

Up
0
Down

hello friend
Import Data from excel to SQL server using C# in ASP. NET
Regard
Priya

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Priyagoel_24 on: 8/1/2013 [Member] Starter | Points: 25

Up
0
Down
The Page having a FileUpload control and the Upload button, on selecting the Excel file user needs to click on Upload button to store the data to Server. Here we are treating the uploaded file as database hence we need to create OLEDB connection to this file, from this connection will be created and the data is fetched to C# as DataTable. '[Sheet1$]' is the Name of the Worksheet where requires data is present.
From this code connection will be created for the Excel file and the data in Sheet1 will be dumped to the dtExcel of type DataTable.
Note:'$' must be there after the worksheet name.
Regard
Priya

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Priyagoel_24 on: 9/14/2013 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class _Default : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["payroll"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
}
protected void button_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into payroll values (nameemployee,[Employee Code],Department,Designation,numberofLeave)";
cmd.Connection = cn;
SqlParameter nameemployee = new SqlParameter("[nameemployee]", SqlDbType.VarChar, 50);
nameemployee.Value = txtnameemployee.Text.ToString();
cmd.Parameters.Add(nameemployee);
SqlParameter employeecode = new SqlParameter("[Employee Code]", SqlDbType.VarChar, 50);
employeecode.Value = txtemployeecode.Text.ToString();
cmd.Parameters.Add(employeecode);
SqlParameter desigation = new SqlParameter("Designation", SqlDbType.VarChar, 50);
desigation.Value = ddlDsg.SelectedItem.Text.ToString();
cmd.Parameters.Add(desigation);
SqlParameter department = new SqlParameter("Department", SqlDbType.VarChar, 50);
department.Value = ddlDept.SelectedItem.Text.ToString();
cmd.Parameters.Add(department);
SqlParameter numberofleave = new SqlParameter("numberofLeave", SqlDbType.VarChar,50);
numberofleave.Value = ddlcl.SelectedItem.Text.ToString();
cmd.Parameters.Add(numberofleave);
SqlParameter numberofleave1 = new SqlParameter("numberofLeave", SqlDbType.VarChar,50);
numberofleave.Value = ddlpl.SelectedItem.Text.ToString();
SqlParameter LeaveofRequestForm = new SqlParameter("LeaveofRequestForm", SqlDbType.VarChar, 50);
LeaveofRequestForm.Value = txtleaveofrequestform.Text.ToString();
cmd.Parameters.Add(LeaveofRequestForm);
//cmd.ExecuteNonQuery();
txtnameemployee.Text = "";
txtemployeecode.Text = "";
ddlDsg.SelectedItem.Text = "";
ddlDept.SelectedItem.Text = "";
ddlpl.SelectedItem.Text = "";
ddlcl.SelectedItem.Text = "";
txtleaveofrequestform.Text = "";





}

}
I my running my project. mmy data is not going on sql database.please tell me. it is urgently

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response