Saturday, July 15, 2023

select query using class and object

 Class  Code


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



using System.Data.SqlClient;


public class my

{

    SqlCommand cm;

    SqlConnection cn;

    SqlDataReader dr;


    public string city1;

    public string msg;

    

public string  display(string na)

    {


        string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";

        cn = new SqlConnection(cs);

        cn.Open();


        string k1 = "select * from demo where name=@name1";

        cm = new SqlCommand(k1, cn);

        cm.Parameters.AddWithValue("name1", na);

        dr = cm.ExecuteReader();

        if(dr.Read())

        {


            city1 = dr["city"].ToString();

            msg = "";

        }

        else

        {

            msg = "Record not found";

        }

        return k1;


    }

}







aspx page  code 


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


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

{

    protected void Page_Load(object sender, EventArgs e)

    {


    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        //Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True

        my m1 = new my();

        string data1 = m1.display(TextBox1.Text);

        TextBox2.Text = m1.city1;

        Label1.Text = m1.msg;


    }

}


No comments:

Post a Comment

GRIDVIEW ON ROW DATA BOUND EVENT

 Database Create  Student : roll , name , city , cost  Fix 6 Value  in Database Record  ====================================================...