Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to display the companyprofile created by a company whem a employer type his username and click on view company profile button.I need to check the username and display the company profile. i written the code as below... but it showing some error.please correct the error...
connectionstring : RegConnectionString
TableName;CompanyProfile

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class viewcompanyprofile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRegConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand("select * from CompanyProfile", con);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
con.Open();
string uname = com.ExecuteScalar().ToString();
DataSet ds = new DataSet();
if (uname == TextBox9usernam.Text)
{
try
{

com.ExecuteNonQuery();
da.Fill(ds, "CompanyProfile");
con.Close();
TextBox1cmpynm.Text = ds.Tables["CompanyProfile"].Rows[0]["CompanyName"].ToString();
TextBox2briefprofle.Text = ds.Tables["CompanyProfile"].Rows[0]["BriefProfile"].ToString();
TextBox3addr.Text = ds.Tables["CompanyProfile"].Rows[0]["Address"].ToString();
TextBox4state.Text = ds.Tables["CompanyProfile"].Rows[0]["State"].ToString();
TextBox5country.Text = ds.Tables["CompanyProfile"].Rows[0]["Country"].ToString();
TextBox6phno.Text = ds.Tables["CompanyProfile"].Rows[0]["PhoneNo"].ToString();
TextBox7website.Text = ds.Tables["CompanyProfile"].Rows[0]["Website"].ToString();
TextBox8emailid.Text = ds.Tables["CompanyProfile"].Rows[0]["EmailID"].ToString();
con.Close();
}
catch (Exception er)
{
Response.Write("Something really bad happend .....Please try again");
}
}
else
{

Response.Write("some problem");
}
}
}
it is not showing error .and also it is not fetching data and displaying it the textbox...
i give
Company Name:(TextBox)
brief details:(TextBox)
Posted
Updated 24-Dec-13 0:40am
v3
Comments
thatraja 24-Dec-13 2:11am    
what's the error message? Always include that in your question
Member 10467514 24-Dec-13 3:37am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class viewcompanyprofile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand("select * from CompanyProfile", con);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
string uname = com.ExecuteScalar().ToString();
DataSet ds = new DataSet();
if (uname == TextBox9usernam.Text)
{
try
{
con.Open();
com.ExecuteNonQuery();
da.Fill(ds, "CompanyProfile");
con.Close();
TextBox1cmpynm.Text = ds.Tables["CompanyProfile"].Rows[0]["CompanyName"].ToString();
TextBox2briefprofle.Text = ds.Tables["CompanyProfile"].Rows[0]["BriefProfile"].ToString();
TextBox3addr.Text = ds.Tables["CompanyProfile"].Rows[0]["Address"].ToString();
TextBox4state.Text = ds.Tables["CompanyProfile"].Rows[0]["State"].ToString();
TextBox5country.Text = ds.Tables["CompanyProfile"].Rows[0]["Country"].ToString();
TextBox6phno.Text = ds.Tables["CompanyProfile"].Rows[0]["PhoneNo"].ToString();
TextBox7website.Text = ds.Tables["CompanyProfile"].Rows[0]["Website"].ToString();
TextBox8emailid.Text = ds.Tables["CompanyProfile"].Rows[0]["EmailID"].ToString();
con.Close();
}
catch (Exception er)
{
Response.Write("Something really bad happend .....Please try again");
}
}
else
{

Response.Write("some problem");
}
}
}
"InvalidoperationException was unhandled by usercode
ExecuteScalar requires an open and available connection .The connection's current state is closed"
JoCodes 24-Dec-13 2:44am    
Debug , check the error msg and add to question.

Hi Add these lines.


C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
       SqlCommand com = new SqlCommand("select * from CompanyProfile", con);
       System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
       DataSet ds = new DataSet();
       con.Open();
       com.ExecuteNonQuery();
       da.Fill(ds, "CompanyProfile");
       con.Close();
 
Share this answer
 
Comments
Member 10467514 24-Dec-13 3:37am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class viewcompanyprofile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand("select * from CompanyProfile", con);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
string uname = com.ExecuteScalar().ToString();
DataSet ds = new DataSet();
if (uname == TextBox9usernam.Text)
{
try
{
con.Open();
com.ExecuteNonQuery();
da.Fill(ds, "CompanyProfile");
con.Close();
TextBox1cmpynm.Text = ds.Tables["CompanyProfile"].Rows[0]["CompanyName"].ToString();
TextBox2briefprofle.Text = ds.Tables["CompanyProfile"].Rows[0]["BriefProfile"].ToString();
TextBox3addr.Text = ds.Tables["CompanyProfile"].Rows[0]["Address"].ToString();
TextBox4state.Text = ds.Tables["CompanyProfile"].Rows[0]["State"].ToString();
TextBox5country.Text = ds.Tables["CompanyProfile"].Rows[0]["Country"].ToString();
TextBox6phno.Text = ds.Tables["CompanyProfile"].Rows[0]["PhoneNo"].ToString();
TextBox7website.Text = ds.Tables["CompanyProfile"].Rows[0]["Website"].ToString();
TextBox8emailid.Text = ds.Tables["CompanyProfile"].Rows[0]["EmailID"].ToString();
con.Close();
}
catch (Exception er)
{
Response.Write("Something really bad happend .....Please try again");
}
}
else
{

Response.Write("some problem");
}
}
}
i added that code and make some changes.then it showed the error
"InvalidoperationException was unhandled by usercode
ExecuteScalar requires an open and available connection .The connection's current state is closed"
Karthik_Mahalingam 24-Dec-13 3:41am    
remove these lines
string uname = com.ExecuteScalar().ToString();
if (uname == TextBox9usernam.Text)
Member 10467514 24-Dec-13 3:53am    
i made a textbox to enter username.after that i placed the view profile button.to check the user i placed the if(uname....)
Member 10467514 24-Dec-13 3:54am    
inside the view profile button i written these codes
Karthik_Mahalingam 24-Dec-13 4:55am    
ExecuteScalar will gives you an integer values.
there is no point of comparing this value with the textbox value...
so remove it..
Quote:
"InvalidoperationException was unhandled by usercode
ExecuteScalar requires an open and available connection .The connection's current state is closed"
It is quite clear.
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand("select * from CompanyProfile", con);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);

con.Open(); // You should open the connection before any ADO.NET Operation.

string uname = com.ExecuteScalar().ToString();
 
Share this answer
 
Comments
Member 10467514 24-Dec-13 4:02am    
I addded the con.open(); above the string uname =......
and then debugg it shows again an error.my database name is database.mdf
An attempt to attach an auto-named database for file C:\Users\IT\Documents\Visual Studio 2010\WebSite\Website nw\App_Data\Databasenw.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Any of the following may be the problem.

-> Connection String is wrong.
-> The mdf file is corrupted.
-> The file/folder/directory does not have sufficient permission for .NET framework.
please mention your error with line and also you write connection in your question because i think error in database, that's may helps get accurate answer..

i will update my answer!!!
 
Share this answer
 
Comments
Member 10467514 24-Dec-13 4:36am    
this error msg shown near con.open();
An@nd Rajan10 24-Dec-13 5:12am    
con.open(); i did't find the line !! check your code and write your connection string ? please ..
Member 10467514 24-Dec-13 6:41am    
i need to display the companyprofile created by a company whem a employer type his username and click on view company profile button.I need to check the username and display the company profile. i written the code as below... but it showing some error.please correct the error...
connectionstring : RegConnectionString
TableName;CompanyProfile

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class viewcompanyprofile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRegConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand("select * from CompanyProfile", con);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
con.Open();
string uname = com.ExecuteScalar().ToString();
DataSet ds = new DataSet();
if (uname == TextBox9usernam.Text)
{
try
{

com.ExecuteNonQuery();
da.Fill(ds, "CompanyProfile");
con.Close();
TextBox1cmpynm.Text = ds.Tables["CompanyProfile"].Rows[0]["CompanyName"].ToString();
TextBox2briefprofle.Text = ds.Tables["CompanyProfile"].Rows[0]["BriefProfile"].ToString();
TextBox3addr.Text = ds.Tables["CompanyProfile"].Rows[0]["Address"].ToString();
TextBox4state.Text = ds.Tables["CompanyProfile"].Rows[0]["State"].ToString();
TextBox5country.Text = ds.Tables["CompanyProfile"].Rows[0]["Country"].ToString();
TextBox6phno.Text = ds.Tables["CompanyProfile"].Rows[0]["PhoneNo"].ToString();
TextBox7website.Text = ds.Tables["CompanyProfile"].Rows[0]["Website"].ToString();
TextBox8emailid.Text = ds.Tables["CompanyProfile"].Rows[0]["EmailID"].ToString();
con.Close();
}
catch (Exception er)
{
Response.Write("Something really bad happend .....Please try again");
}
}
else
{

Response.Write("some problem");
}
}
}
it is not showing error .and also it is not fetching data and displaying it the textbox...
i give
Company Name:(TextBox)
brief details:(TextBox)
Improve question Permalink
An@nd Rajan10 24-Dec-13 5:19am    
Please update your question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900