This asp.net code is not fetching any data why? - asp.net

DataTable table = new DataTable();
DataSet ds = SqlHelper.ExecuteDataset(Utility.GetPumaConString(),
CommandType.Text, #"SELECT name,IsBlocked FROM
ht_cust where type=14 and DealerId<>19");
return ds.Tables[0];

Kindly try this on your project.
SqlConnection connection = new SqlConnection("your connectiongstring");
SqlCommand cmd = new SqlCommand("SELECT name,IsBlocked FROM ht_cust where type=14 and DealerId<>19", connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
connection.Open();
da.Fill(dt);
//bind it to the grid
gv.DataSource = dt;
gv.DataBind();
connection.Close();

Related

i got an da.Fill(ds) error for my shopping card project in asp.net c#

public partial class AddToCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("sno");
dt.Columns.Add("ProductID");
dt.Columns.Add("ProductName");
dt.Columns.Add("Price");
dt.Columns.Add("ProductImage");
dt.Columns.Add("Cost");
dt.Columns.Add("TotalCost");
if (Request.QueryString["id"] != null)
{
if (Session["Buyitems"] == null)
{
dr = dt.NewRow();
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True";
SqlConnection scon = new SqlConnection(mycon);
String myquery = "select * from productdetail where ProductID=" + Request.QueryString["id"];
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = scon;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = 1;
dr["ProductID"] = ds.Tables[0].Rows[0]["ProductID"].ToString();
dr["ProductName"] = ds.Tables[0].Rows[0]["ProductName"].ToString();
dr["ProductImage"] = ds.Tables[0].Rows[0]["ProductImage"].ToString();
dr["Price"] = ds.Tables[0].Rows[0]["Price"].ToString();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
}
else
{
dt = (DataTable)Session["buyitems"];
int sr;
sr = dt.Rows.Count;
dr = dt.NewRow();
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True";
SqlConnection scon = new SqlConnection(mycon);
String myquery = "select * from productdetail where ProductID=" + Request.QueryString["id"];
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = scon;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = sr + 1;
dr["ProductID"] = ds.Tables[0].Rows[0]["ProductID"].ToString();
dr["ProductName"] = ds.Tables[0].Rows[0]["ProductName"].ToString();
dr["ProductImage"] = ds.Tables[0].Rows[0]["ProductImage"].ToString();
dr["Price"] = ds.Tables[0].Rows[0]["Price"].ToString();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
}
}
else
{
dt = (DataTable)Session["buyitems"];
GridView1.DataSource = dt;
GridView1.DataBind();
Update your connection string like below. You are missing to specify whether you want to use Windows Authentication or User Id & Password.
For Windows Authentication use Integrated Security=SSPI as below :
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=SSPI";
For Authentication with User Id & Password add User Id & Password as below. Use original user id and password. I have taken sa for example. :
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True;user id=sa;password=sa";
Also you need to open connection with
SqlConnection scon = new SqlConnection(mycon);
scon.Open(); //Open connection
P.S. It is always recommended to Close connection too. Add scon.Open(); after da.Fill(ds); line. Why always close Database connection?
.
da.Fill(ds);
scon.Close(); //Close connection

How to Implement Multiple search in Asp.Net

I want to Implement multiple search query system in Asp.Net where search input are in form of TEXTBOX and DROPDOWN LIST. Query should work in combination or indivisually to filter the data from SQL Server
and show in Gridview.
This Code Snippet is for filtering two Dropdown values:
if (Agree_type_srch.SelectedValue != null || Status_srch.SelectedValue != null)
{
if (Agree_type_srch.SelectedValue != null)
{
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand com = new SqlCommand("Select *from EntryDatabase where Agree_type ='" + Agree_type_srch.SelectedItem.Text + "'", conn);
SqlDataAdapter sqldatad = new SqlDataAdapter();
DataSet ds = new DataSet();
com.Connection = conn;
sqldatad.SelectCommand = com;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
else if (Status_srch.SelectedValue != null)
{
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand com = new SqlCommand("Select *from EntryDatabase where Curnt_St ='" + Status_srch.SelectedItem.Text + "'", conn);
SqlDataAdapter sqldatad = new SqlDataAdapter();
DataSet ds = new DataSet();
com.Connection = conn;
sqldatad.SelectCommand = com;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
if (Agree_type_srch.SelectedItem.Text != null && Status_srch.SelectedItem.Text != null)
{
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand com = new SqlCommand("Select * from EntryDatabase where Agree_type ='" + Agree_type_srch.SelectedItem.Text + "'and Curnt_St ='" + Status_srch.SelectedItem.Text + "'", conn);
SqlDataAdapter sqldatad = new SqlDataAdapter();
DataSet ds = new DataSet();
com.Connection = conn;
sqldatad.SelectCommand = com;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
...
First, using string concatenation to provide parameters can result in SQL injection, use SqlParameter to pass parameters would be better.
Second, consider to warp all SqlClient classes by using scope so you don't have to worry close/dispose.
Lastly, For your question, you can use WHERE 1=1 then append any conditions you need.
Take your code as instance.
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
string query = "SELECT * FROM EntryDatabase WHERE 1=1";
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
if (Agree_type_srch.SelectedValue != null)
{
query += " AND Agree_type = #agree_type";
cmd.Parameters.AddWithValue("agree_type", Agree_type_srch.SelectedValue);
}
if (Status_srch.SelectedValue != null)
{
query += " AND Curnt_St = #curnt_st";
cmd.Parameters.AddWithValue("curnt_st", Status_srch.SelectedValue);
}
cmd.CommandText = query;
using (SqlDataAdapter sqldatad = new SqlDataAdapter())
{
DataSet ds = new DataSet();
sqldatad.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}

asp.net Dropdown list databinding in runtime

con = new SqlConnection(s);
con.Open();
if (RadioButtonList1.SelectedIndex == 0)
{
cmd = new SqlCommand("select [Item] from Veg_Items", con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "[Item]");
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();
}
else if (RadioButtonList1.SelectedIndex == 1)
{
cmd = new SqlCommand("select [Item] from NonVeg_Items", con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "[Item]");
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();
}
con.Close();
}
I have items in my table and I need my items to be displayed in Dropdownlist once I select any value in RadioButtonList.
I also visualized the items in ds.Tables[0] line but I can't bind them to Dropdownlist.
con.Open();
if (RadioButtonList1.SelectedIndex == 0)
{
cmd = new SqlCommand("select [Item] from [Veg_Items]", con);
SqlDataReader dr = cmd.ExecuteReader();
List<Object> lt = new List<Object>();
if (dr.HasRows)
{
while (dr.Read())
{
lt.Add(dr["Item"].ToString());
}
}
DropDownList1.DataSource = lt;
DropDownList1.DataBind();
}
else if (RadioButtonList1.SelectedIndex == 1)
{
cmd = new SqlCommand("select [Item] from [NonVeg_Items]", con);
SqlDataReader dr = cmd.ExecuteReader();
List<Object> lt = new List<Object>();
if (dr.HasRows)
{
while (dr.Read())
{
lt.Add(dr["Item"].ToString());
}
}
DropDownList1.DataSource = lt;
DropDownList1.DataBind();
}
con.Close();

How to display count(*) to label text in asp.net using c#.net

How to display count(*) to label text in asp.net using c#.net i mean tbl1 is Having 10 rows that 10 value set to label text
con.Open();
SqlCommand cmd = new SqlCommand("select count(newsid)from tbl_news where newsid=44,con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
lbl_so.Text = ds.Tables[0].Rows.Count.ToString();
try this one,
con.Open();
SqlCommand cmd = new SqlCommand("select count(newsid) as totalRow from tbl_news where newsid=44,con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
lbl_so.Text = ds.Tables[0].Rows[0]["totalRow"].ToString();

Dropdown sets as System.Data.DataRow, What is wrong with my code?

System.Data.DataTable dt = new System.Data.DataTable();
CDbAccess db = new CDbAccess();
IDbConnection conn = db.GetConnectionInterface();
conn.Open();
string str = "select eName from bt_modules";
IDbCommand cmd = db.GetCommandInterface(str);
IDbDataAdapter da = db.GetDataAdapterInterface(cmd);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
DropDownList2.DataSource = dt;
DropDownList2.DataBind();
conn.Close();
The above code set Dropdown items as System.Data.DataRow
How to get the actual values?
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "eName";
DropDownList2.DataBind();
conn.close();
You can refer this code also http://www.aspdotnet-suresh.com/2012/10/showbind-data-to-aspnet-dropdownlist.html
try like this :
DropDownList2.DataSource = dt;
DropDownList1.DataTextField = "eName " '// Column Name
DropDownList2.DataBind();
If you want to use value field also using like this
DropDownList2.DataSource = dt;
DropDownList1.DataTextField = "eName " '// Column Name
DropDownList1.DataValueField = "eid" '// Column Name
DropDownList2.DataBind();

Resources