Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,
I am designing a webPage..Kindly read below points for my requirements.....

I have a button,on the click event of button,I want a row to be fetched from SQL server 2008.......

Colums in my SQL table is

Source, destination, date, time, summary

I want to fetch summary based on Source, destination, date, time from SQL server 2008......and pass it to some function in JavaScript....

I don't know how to connect to SQL server,and fetch a row from the SQL server....

please help..
Posted
Updated 7-Oct-12 2:20am
v2

User Name Space -

using System.Data.SqlClient;


Make connection to Data Base : Use SQLConnection Class.

SqlConnection con = new SqlConnection("server=Harshit-PC; database=YourDataBaseName; uid=YourSQLuid; pwd=YourSQLPassword");


Wants Records On Button Click Then :

Your Need Two Object -
(i) One For Executing Command (SqlCommand cmd)
(ii) Other For Reading Data (SqlDataReader dr)


C#
con.Open(); //Open Connection

        string FetchData = "Select * from cus where Name='fb'";
        cmd = new SqlCommand(FetchData, con);  // Passing Query
        dr = cmd.ExecuteReader();      // Executing Command & Reading Data

        if (dr.Read())
        {

            TextBox1.Text = dr[0].ToString(); //  Use Data From Here
            TextBox2.Text = dr[1].ToString();//   Use Data From Here
        }



Sample Code For fetching data from sql server and inserting into asp.net textbox


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 Ask Me For Any Query
 
Share this answer
 
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
 
Check out this video- its for sql server 2005 but it makes no difference for you.

http://www.youtube.com/watch?v=8FE3hqn9z1E[^]

On the button click you need to send get/post request. I will advise you to use ajax. Just google around how based on which kind of GUI architecture you are using.
 
Share this answer
 
v3

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