Click here to Skip to main content
15,924,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my page is like this,
CompanyName (here is a textbox id:TextBox9cmpynam)
view CompanyProfile(button)
when we click this button the data from database must displayed in the corresponding textbox..any one can correct my code...
C#
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_Click1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRegConnectionString"].ConnectionString);
        SqlCommand com = new SqlCommand("select * from CompanyProfile where CompanyName'" + TextBox9cmpynam.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        con.Open();
        DataSet ds = new DataSet();
        string cmdStr = "select count(*) from CompanyProfile where CompanyName='" + TextBox9cmpynam.Text + "'";
        SqlCommand User = new SqlCommand(cmdStr, con);
        int temp = Convert.ToInt32(User.ExecuteScalar().ToString());
        con.Close();
        if (temp == 1)
        {
           try
            {

                Panel1.Visible = true;
                com.ExecuteNonQuery();
                da.Fill(ds, "CompanyProfile");
                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][""].ToString();
                con.Close();
            }
            catch (Exception er)
            {
                Response.Write("Something really bad happend .....Please try again");
            }
        }
        else
        {

            Panel1.Visible = false;

        }
    }
}

when i executed this code there is no error .but it is not displaying the data from database to textbox
Posted
v2
Comments
JoCodes 25-Dec-13 3:26am    
Have check the same sql query in Query Analyzer ?. Execute it and check whether it returns any rows in the DB..
Member 10467514 25-Dec-13 3:33am    
Query Analyzer??...I am A fresher in this field.......
JoCodes 25-Dec-13 3:50am    
Its just to execute the sql query in the sqlserver query analyser window to check the sql statement works fine or not.

Please don't use inline queries, it opens your code to Sql Injection attack.

So, change the following query...
C#
string cmdStr = "select count(*) from CompanyProfile where CompanyName='" + TextBox9cmpynam.Text + "'";

to...
C#
string query = "select * from CompanyProfile where CompanyName=@CompanyName";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@CompanyName", TextBox9cmpynam.Text);

After that debug your code and find if there are any rows present in the DataSet ds when the following code line executes..
C#
da.Fill(ds, "CompanyProfile");


[Update]
Use the below code...
C#
protected void Button1_Click1(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRegConnectionString"].ConnectionString);
    
    string query = "select * from CompanyProfile where CompanyName = @CompanyName";
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.Parameters.Add(new SqlParameter("@CompanyName", TextBox9cmpynam.Text.Trim()));
    
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();     
    
    try
    {
    	da.Fill(ds, "CompanyProfile");
    
    	if(ds != null && ds.Tables["CompanyProfile"] != null && ds.Tables["CompanyProfile"].Rows.Count > 0)
    	{
    		Panel1.Visible = true;
    		
    		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][""].ToString(); // Here provide the Column Name.
    		con.Close();
    	}
    	else
        {
            Panel1.Visible = false;
        }
    }
    catch (Exception er)
    {
    		Response.Write("Something really bad happend .....Please try again");
    }    
}
 
Share this answer
 
v7
Comments
Member 10467514 25-Dec-13 3:16am    
when i added the above command it shows error
sql exception handled
you must declare the @CompanyName
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_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRegConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand("select * from CompanyProfile where CompanyName'" + TextBox9cmpynam.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(com);
con.Open();
DataSet ds = new DataSet();
string query = "select count(*) from CompanyProfile where CompanyName=@CompanyName";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@CompanyName", TextBox9cmpynam.Text);
SqlCommand User = new SqlCommand(query, con);
int temp = Convert.ToInt32(User.ExecuteScalar().ToString());
con.Close();
if (temp == 1)
{
try
{

Panel1.Visible = true;
com.ExecuteNonQuery();
da.Fill(ds, "CompanyProfile");
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][""].ToString();
con.Close();
}
catch (Exception er)
{
Response.Write("Something really bad happend .....Please try again");
}
}
else
{

Panel1.Visible = false;

}
}
}
You have made it very complex.

I am updating my answer, use that. :)
Member 10467514 25-Dec-13 4:02am    
sorry to tell that....it showing the panel containing textbox but not showing the correspoding company name profile details in textbox.textbox is showing empty.....
Sorry, my mistake. There was a problem in query. There should not count(*).

It should be...

string query = "select * from CompanyProfile where CompanyName = @CompanyName";

But I would suggest you to write the Column Names separated by comma instead of *.

Now, I have updated the answer. It should work. Try and let me know.
Member 10467514 25-Dec-13 6:07am    
now it is showing value only in the company name textbox...
my page is shown below
CompanyName (here is one textbox)
view my profile(button)


CompanyName (textbox)
BreifProfile (textbox)
Address (textbox)
State (textbox)
Country (textbox)
PhoneNo(textbox)
Website (textbox)
EmailID(textbox)
here when we click view button only Company Name is displaying...other textboxes are showing empty...
you make a Store Procedure and call to one by one. your code is very confused crate.You make sure your sql Query is work fine. Once again try with the help of Brake Point and check line by line. for more information try this link http://csharp.net-tutorials.com/debugging/breakpoints/[^]
 
Share this answer
 
Comments
Member 10467514 25-Dec-13 3:31am    
can you please say how to add a checkbox to the exsisting gridview.........
joginder-banger 25-Dec-13 4:02am    
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<columns>
<asp:TemplateField>
<itemtemplate>
<asp:CheckBox ID="CheckBox3" runat="server" />



<emptydatatemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />

Try

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CRegConnectionString"].ConnectionString);
    SqlCommand com = con.CreateCommand();
    com.CommandText ="select * from CompanyProfile where CompanyName'" + TextBox9cmpynam.Text + "'";
    con.Open();
SqlDataAdapter da = new SqlDataAdapter(com);
        con.Open();
        DataSet ds = new DataSet();
	da.Fill(ds,"CompanyProfile");
    int temp = ds.Tables[0].Rows.Count;
    if (temp == 1)
        {
		//Logic based on condition
	}

    con.Close();


Note that Tadit , already has pointed out that you missed to fill your Dataset .
Also, avoid inline queries which are open to SQL injections.


Hope this helps...
 
Share this answer
 

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