Select Query [ Login page ]

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls; 

using System.Data.SqlClient;

public partial class Registration : System.Web.UI.Page

{   SqlConnection cn;

    SqlCommand cm;

    SqlDataReader dr;


    protected void Page_Load(object sender, EventArgs e)

   {

    protected void Button1_Click(object sender, EventArgs e)

 { //step 1- connection to database

 string path =@"Data Source=49.50.88.43;Initial Catalog=grkistdb;Persist Security Info=True;User ID=grkistuser;Password=q2oR5w_356";

     cn = new SqlConnection(path);

     cn.Open();


//step 2- Command (write a query)

        string q = " select name, pass from Chitransh_stu where name= '"+TextBox2.Text+"' and pass = '"+TextBox6.Text+"'"; 

cm = new SqlCommand(q, cn);


  //step 3- execute the query. 

 dr= cm.ExecuteReader();

if(dr.Read()) 

Response.Write("login successfully done.."); 

 }   


else 



dr.Close(); 


Response.Write("Records not found"); 



 } 


}           


Note:- Connection code is similar for all queries. 

Select is an Execute Query then datareader comes into picture. 


DataReader:-  

> Firstly the query will be stored in a variable (q)

> The variable will be passed into an object (cm)

> data reader is used to read the query or check whether the record is present in the table or not. 

> dr = cm.ExecuteReader(); 

> For Checking, we use conditional statement- 

   if(dr.Read())  

> Then Show a message "login successfully done". 

> If the data reader finds no record with the roll no 

  Then show "No record found".  

> And Remember to close the datareader .

else

dr.Close(); 

??(records not found); 










    


        




    





Comments

Popular posts from this blog

Parameter Query

Final Project

Grid View Paging