Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
how to initialize sql conncetion on page_load

-- Edit Added Code from Solution --
Code Behind :

C#
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.Collections.Generic;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
SqlConnection con = new SqlConnection(@"Data Source=SERVER NAME;Initial Catalog=DB NAME;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from TABLENAME where TicketID=" + TextBox1.Text + "",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
con.Close();
}
}


New Code

C#
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_select",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TicketId", TextBox1.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
con.Close();
}


SQL
create proc sp_select
@TicketId bigint
as begin
select * from TABLENAME where TicketId=@TicketId


C#
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_select",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TicketId", Convert.ToInt64(TextBox1.Text));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
con.Close();
}
Posted
Updated 31-Aug-11 4:38am
v3
Comments
Kschuler 31-Aug-11 10:36am    
Do not post your question code as a solution. Instead, click the Improve Question button and edit your question to include your code. Also, don't just post code...explain what you were trying to do in the code and why it doesn't work. Also include error messages that you are getting.
nagendrathecoder 5-Sep-11 2:49am    
I guess this is his general practise. If u look his previous questions, only he has answered those that too 4-5 times :D
Kschuler 31-Aug-11 10:39am    
I updated your question for you and deleted your solutions with code. Please update your question again with more information.
Uday P.Singh 31-Aug-11 12:26pm    
what is your issue?

If you want to get data on page_Load, then put that code into page_load, not in Button_click
 
Share this answer
 
I guess you want to do something like below.

C#
SqlConnection con = null;
protected void Page_Load(object sender, EventArgs e)
{
  con = new SqlConnection(@"Data Source=SERVER NAME;Initial Catalog=DB  NAME;Integrated Security=True");
}
 
Share this answer
 
hai da kindly chk this coding da.....



C#
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.Collections.Generic;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    
    protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=10.242.17.143;Initial Catalog='CCloud_Metlife_21JanQA'; uid='Cclouddev';pwd=password-12;");
        con.Open();
        SqlCommand cmd = new SqlCommand("getexception", con);
        cmd.CommandType=CommandType.StoredProcedure;
        SqlParameter sqlParam = new SqlParameter();
        sqlParam.ParameterName = "@TicketID";
        sqlParam.DbType = DbType.Int64;
        sqlParam.Value=TextBox1.Text;
        sqlParam.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(sqlParam);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            GridView1.DataSource = dr;
            GridView1.DataBind();
        }
        con.Close();
    }
}
 
Share this answer
 
v2

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