Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.14/5 (3 votes)
how can i fetch data from sql server 2008 and insert that all into different text box. please help me on this
Posted
Comments
[no name] 7-Oct-12 8:57am    
You connect to your server, query the tables for the data the you want to display, and then assign the results to your textboxes.

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

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection("server=Harshit-PC; database=Temp; uid= sa;pwd=india");
    SqlDataReader dr;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();

        string FetchData = "Select * from cus where Name='fb'";
        cmd = new SqlCommand(FetchData, con);
        dr = cmd.ExecuteReader();

        if (dr.Read())
        {

            TextBox1.Text = dr[0].ToString();
            TextBox2.Text = dr[1].ToString();
        }



    }
}


Please tell me if not working !!!
 
Share this answer
 
v2
Comments
damodara naidu betha 11-Oct-12 5:54am    
Correction..

while(dr.Read())
{
TextBox1.Text +=dr[0].ToString()+" ";
TextBox2.Text +=dr[1].ToString()+" ";
}
con.close()
Well, by using a Connection String[^] you can connect to database.

Further info regarding the data communication can be read here:
MSDN: Accessing data with ADO.NET[^]
Beginners guide to accessing SQL Server through C#[^]
 
Share this answer
 
On Search Click

DataTable dt = new DataTable();
//SqlDataAdapter(Query,Connection)
SqlDataAdapter adp = new SqlDataAdapter("Select * from TableName where EMPID="+textBox1.Text+" ",con);
adp.Fill(dt);
// dt.Rows[index][ColumnName]
textBoxEmpName.text = dt.Rows[0]["Emp_Name"].ToString();

On Update Click


try
{
//SqlCommand(query,connection)
SqlCommand cmd = new SqlCommand("Update tabel TableName set EmpName='" + textBoxEmpName.Text + "' where EMPID="+textBox1.Text+"", con)
;
cmd.ExecuteNonQuery();
}
catch(Exception c)
{
}


try this
 
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