Sql Server Database in asp.net
Search example in asp.net (Web.Config file and Class File in Asp.net)
Search.aspx Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Search : System.Web.UI.Page
{
protected voidSearchDatainGridviewButton2_Click(objectsender, EventArgs e)
{
String searchcmd = "select *from ProductTable where ProductId=" + TextBox1.Text;
ClsShoping obj = newClsShoping();
DataSet ds = new DataSet(); ds = obj.ReturnDs(searchcmd);
GridView1.DataSource = ds.Tables[0]; GridView1.DataBind();
}
protected voidSearchDatainTextBoxButton1_Click(object sender, EventArgs e)
{
String searchcmd = "select *from ProductTable where ProductId=" + TextBox1.Text;
ClsShoping obj = newClsShoping();
DataSet ds = new DataSet();
ds = obj.ReturnDs(searchcmd);
TextBox2.Text = ds.Tables[0].Rows[0]["ProductName"].ToString();
TextBox3.Text = ds.Tables[0].Rows[0]["ProductImage"].ToString();
TextBox4.Text = ds.Tables[0].Rows[0]["Remarks"].ToString();
Image1.ImageUrl = TextBox3.Text;
}
protected voidSearchNamewiseButton3_Click(object sender, EventArgs e)
{
String searchcmd = "select *from ProductTable where ProductName like '"+ TextBox2.Text+"%'";
ClsShoping obj = newClsShoping();
DataSetds = new DataSet();
ds = obj.ReturnDs(searchcmd);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
web.config file
Adding Web.Config File:
web.config file Code:-
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="Strcon" connectionString="Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True"/>
</connectionStrings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
</system.web>
</configuration>
Adding Class:-
ClsShoping.cs
Class Code:-
using System;
using System.Collections.Generic;
using System.Linq; using System.Web;
using System.Data; using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for ShopingCartClass
/// </summary>
public class ClsShoping
{
SqlConnectioncon = new SqlConnection(ConfigurationManager.ConnectionStrings["Strcon"].ToString());
public DataSetReturnDs(String sqlcmd)
{
con.Open();
SqlDataAdapter da = newSqlDataAdapter(sqlcmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
return ds;
}
public int ForQuery(String strcmd)
{
con.Open();
SqlCommand cmd = newSqlCommand(strcmd, con);
int x = cmd.ExecuteNonQuery();
con.Close();
return x;
}
}
Comments
Post a Comment