Click here to Skip to main content
11,410,750 members (63,592 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 7-Oct-12 3:16am
Edited 7-Oct-12 3:20am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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#[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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)


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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,920
1 OriginalGriff 6,918
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 7 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100