简体   繁体   中英

How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net

My question is

Suppose I have a Column "fname" whose value is 'Nikhil' in table "profile".

How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net.

I mean what should be the code if I want label text to be "fname" value that is "Nikhil"

Connection is already done properly because I am able to display table data in Gridview.

label1.Text = ?; // I want fname here

Regards,

Nikhil

Go to MSDN to learn it http://msdn.microsoft.com/en-us/bb188199 .

Here's a sample on how to connect to a database.

    private static void ReadOrderData(string connectionString)
    {
        string queryString =
            "SELECT OrderID, CustomerID FROM dbo.Orders;";

        using (SqlConnection connection =
                   new SqlConnection(connectionString))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data.
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }

            // Call Close when done reading.
            reader.Close();
        }
    }

There are many resources out there, try to search first before posting question.

Firstly, I established a connection

SqlConnection con = new SqlConnection("data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true");
SqlCommand cmd = new SqlCommand();

and then,

cmd.CommandText = "select fname from table where qid=1";
cmd.Connection = con;
string fname = ((string)cmd.ExecuteScalar());

or

Label1.text = ((string)cmd.ExecuteScalar());

First Create A connection Class in App_code folder and dont forget to set database path

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

/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection

{
    SqlConnection con = new SqlConnection();
    SqlDataAdapter ad;
    SqlCommand cmd;
    SqlDataReader rd;
    public Connection()
    {
        // Set Your Database Path Here from C:\user onwords
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\SIROI.COM\Documents\Visual Studio 2008\WebSites\WebSite14\App_Data\ASPNETDB.MDF;Integrated Security=True;User Instance=True";

    }

    public DataSet filldata(DataSet ds, string query, string tbname)
    {
        try
        {
            con.Open();
            ad = new SqlDataAdapter(query, con);
            ad.Fill(ds, tbname);

        }
        catch (SqlException ex)
        {
        }
        finally
        {
            con.Close();
        }
        return ds;
    }
    public bool ExecuteQuery(string query)
    {
        bool flag = false;
        try
        {
            con.Open();
            cmd = new SqlCommand(query, con);
            int a = cmd.ExecuteNonQuery();
            if (a > 0)
            {
                flag = true;
            }          
        }
        catch(Exception ex)
        {

        }
        finally
        {
            con.Close();
        }
        return flag;
    }
    public SqlDataReader ExecuteReader(string query)
    {
        try
        {
            con.Open();
            cmd = new SqlCommand(query, con);
            rd = cmd.ExecuteReader();
        }
        catch (Exception ex)
        {

        }

        return rd;
    }
}

Now create data source by calling connection

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>" 
    SelectCommand="Your * SQL Query">
    <SelectParameters>
        <asp:QueryStringParameter Name="Your param" QueryStringField="Your Field" 
            Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

Now at last Create a Label and set field name to retrive in Bind Function

<asp:Label ID="Label6" runat="server" Text='<%# Bind("your Field") %>'></asp:Label>

Regards http://www.siroi.com Dont Forget to like us on Facebook http://www.facebook.com/siroi.india

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM