Regarding binding array to gridview - asp.net

Hi
I have created session array :
int[] a = (int[])Session["values"];
Now i have to bind this value to my gridview. I have one column (boundfield in my gridview) in gridview.
I used code to bind array to gridview as follows, but its giving last value only as i want all the values to bind :
for (int i = 0; i < a.Length; i++)
{
str = "select * from Quest_Info where Quest_id='" + a[i] + "' order by Quest_id";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
ds2 = new DataSet();
da2.Fill(ds2, "Result");
reviewgrid.DataSource = ds2;
reviewgrid.DataBind();
}
Which code will work for this?
Asp.net, c#
Thank You.

Your code may not work because you are trying to bind gridview in the for loop which keep repeating and will result to bind only one last array id in the end.
You may try to prepare query out front before binding the gridview as example below.
string questIds = string.Empty;
for (int i = 0; i < a.Length; i++)
{
if (questIds.Length > 0)
questIds += ", ";
questIds += a[i];
}
string strSQL = "select * from Quest_Info where Quest_id IN ("+ questIds + ") order by Quest_id";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
ds2 = new DataSet();
da2.Fill(ds2, "Result");
reviewgrid.DataSource = ds2;
reviewgrid.DataBind();

Related

Get Distinct data from datatable present in webservices using linq

I want to get only single row from multiple rows with same projectname from the datatable.(Eg. if we have two rows with same projectname,the datatable should be loaded with the only one row and neglect the other one.).I have been using webservices which has the datatable.
I want to achieve this functionality using linq.
I have pasted my code for datatable.Pls help me with working code.
[WebMethod]
public DataTable Get()
{
int a = 0;
cmd = con.CreateCommand();
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = " Select PROJECTNAME,COMPANY,PROJECTSTATUS,STARTEDIN,COMPLETEDIN FROM CMPPROJECT WHERE STATUS ='" + a + "'";
using (OracleDataAdapter sda = new OracleDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
dt.TableName = "CMPPROJECT";
sda.Fill(dt);
return dt;
}
}
}
You can create a DataView object which has a method ToTable in which you can pass true to parameter distinct to select distinct rows. But this has no sense to me. I would do this directly in a select query:
DataTable d = new DataTable("CMPPROJECT");
d.Columns.Add("PROJECTNAME");
d.Columns.Add("COMPANY");
d.Rows.Add(1, 1);
d.Rows.Add(1, 1);
d.Rows.Add(2, 2);
d = new DataView(d).ToTable("CMPPROJECT", true, "PROJECTNAME", "COMPANY");
Here is `linq solution:
var select = (from a in d.AsEnumerable()
select new { c1 = a["PROJECTNAME"], c2 = a["COMPANY"] }).Distinct().ToList();
d.Clear();
foreach (var item in select)
d.Rows.Add(item.c1, item.c2);

Get Selected index Change Event for Each row from grid view

I had tried binding dynamically created drop down list inside grid view and also added the selected index change event for it but it is giving me output when i had change value of first row drop down. i want individual rows selected index change event what can i do?
I had taken grid view in update panel and added dynamically drop down list in grid view and also bind it for each row.now i want to add selected index change event for each rows drop down list . my code is as follows:-
for (int i = 0; i < gv_details.Rows.Count; i++)
{
DropDownList ddl_Hotel = new DropDownList();
ddl_Hotel.ID = "ddl_Hotel_" + i;
//add TextBox to the first column
string loc = gv_details.Rows[i].Cells[3].Text.ToString();
string selecctloc = "select locid from loc_master where location='" + loc + "'";
SqlDataReader dr = cdf.Execute_SelectForDR(selecctloc);
dr.Read();
string loc_id = dr["locid"].ToString();
dr.Close();
string bind_hotelList = "SELECT Hotel_Id,Hotel_Name FROM Mast_Hotel WHERE LocId ='" + loc_id + "'";
cdf.Bind_DropDownList(ddl_Hotel, bind_hotelList, "Hotel_Name", "Hotel_Id");
//ddl_Hotel.Items.Add(new ListItem("Select", "0"));
ddl_Hotel.Items.Insert(0, new ListItem("---Select---", "0"));
ddl_Hotel.SelectedValue = "0";
gv_details.Columns[5].Visible = true;
ddl_Hotel.SelectedIndexChanged += new EventHandler(ddl_Hotel_SelectedIndexChanged);
this.Form.Controls.Add(ddl_Hotel);
UpdatePanel1.ContentTemplateContainer.Controls.Add(ddl_Hotel);
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = ddl_Hotel.ID;
trigger.EventName = "SelectedIndexChanged";
UpdatePanel1.Triggers.Add(trigger);
ddl_Hotel.AutoPostBack = true;
gv_details.Rows[i].Cells[5].Controls.Add(ddl_Hotel);
// ddl_Hotel.Attributes.Add("onchange", "retun destination_name()");
}
please suggest me the answer.

Error cannot find Column 2 dr = ds.Tables[0].Rows[0]

I've been searching for an answer since this code was working fine like an hour ago and I did not make any editting so it turns a little confuse to recognize the error if any of you could help I would appreciate it much. Thanks in advance.
I've checked and there are no errors on the SQL query, as I said it was working fine dr is defined globally as:
DataRow dr = new DataRow();
and MyTable is defined globally also as
DataTable MyTable = new DataTable();
here's the related code:
sentencia = "select * from Facturas" + Session["sociotabla"].ToString() + " where IdFactura = " + Session["idFactura"].ToString();
ds = bd.Consulta(sentencia);
if (ds != null)
{
dr = MyTable.NewRow();
dr = ds.Tables[0].Rows[0];
LimpiarControles();
tbFactura.Text = dr[0].ToString();
ListItem li;
for (int i = 0; i < ddlCliente.Items.Count; i++)
{
li = ddlCliente.Items[i];
if (li.Value == dr[2].ToString()) //Error here Cannot find Column 2
{
ddlCliente.SelectedIndex = i;
Session["idCliente"] = dr[2].ToString();
break;
}
}

Exporting data from a table in SQL Server using ASP.Net

I am using Visual Studio 2010 and SQL Server 2008. I need to create a XML file which looks like:
<cat>
<name> "Categories"</name>
<link> "link"</link>
</cat>
The Categories and link values should be added from the database.
I have tried everything but I cannot get it to work. DO I have to create a XML file in ASP to do this?
These values are in the same table but there are other columns in the table as well.
My code looks something like this:
SqlCommand sqlcmd = new SqlCommand();
//sqlcmd.CommandText = "Select * from Categories";
DataSet ds = new DataSet();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * from Categories", "Data Source=something;Initial Catalog=My Database;Integrated Security=True");
da.Fill(ds);
int rows;
rows = ds.Tables[0].Rows.Count;
int i;
for (i = 0; i <= rows - 1; i++)
{
string Categories = ds.Tables[0].Rows[i].ItemArray[0].ToString();
string address = "https://www.something.com/";
string link = address + ds.Tables[0].Rows[i].ItemArray[1].ToString();
}
ds.WriteXml(#"c:\output.xml", XmlWriteMode.WriteSchema);
Can someone please give me a detailed solution to this problem.
Thank you
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "Select * from Categories";
DataSet ds = new DataSet();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * from Categories", "Data Source=something;Initial Catalog=My Database;Integrated Security=True");
da.Fill(ds);
int rows;
rows = ds.Tables[0].Rows.Count;
int i;
xmlwr.WriteStartElement("Cat");
for (i = 0; i <= rows - 1; i++)
{
xmlwr.WriteStartElement("name");
xmlwr.xmlwr.WriteString(ds.Tables[0].Rows[i].ItemArray[0].ToString());
xmlwr.WriteEndElement;
xmlwr.WriteStartElement("link");
xmlwr.xmlwr.WriteString(ds.Tables[0].Rows[i].ItemArray[1].ToString());
xmlwr.WriteEndElement;
}
xmlwr.WriteEndElement;
ds.WriteXml(#"c:\output.xml", XmlWriteMode.WriteSchema);

Dynamic parameter and query

I'm creating a search engine for my asp page.There are 15 pieces filtering options like Price,Date,Name etc.
Also,The use of Filtering Options is optional.If the filtering options are left blank,search will be made.
I found a solution by using if and string.
string sorgu = "";
dbcommand cmd = CommandClassım.YeniCommand();
sorgu += "Select * From Urunler Where ";
if(UrunAdi.Text != string.Empty)
{
sorgu += "UrunAdi = #UrunAdi";
dbparameters prm = cmd.createparameter();
prm.Parametername = "#UrunAdi";
prm.Value = UrunAdi.Text;
prm.DbType = DbType.String;
cmd.Parameters.Add(prm);
}
cmd.CommandText = sorgu;
it's running upper code without problem.However,I want to using a parameter to filter dynamic.
So,the user choose those categories through checkbox inside search engine.
When I try to run below code,I got the following error.
Must declare the scalar variable "#cat0".
Code:
string sorgu = "";
DbCommand cmd = CommandClassım.YeniCommand();
sorgu += "Select * From Urunler Where ";
DbParameter prm = cmd.CreateParameter;
for(int i = 0; i < Category.Length; i++)
{
sorgu += "Kategori = #cat" + i.ToString();
prm.ParameterName = "#cat" + i.ToString();
prm.Value = Category.Value;
prm.DbType = DbType.String;
cmd.Parameters.Add(prm);
}
cmd.CommandText = sorgu;
Just a hunch, but could it be that you're not providing the indexer for Category?
prm.Value = Category.Value;
I would think it should be:
prm.Value = Category[i].Value;

Resources