VS 2010 - Gridview - Fetch Single Column Values From Sqldatasource
Jan 26, 2011
i am using gridview where it fetches datas from sqldatasource, i want to made some calculations wit some fields from tables and then need to show in gridview. How to fetch single column values from sqldatasource. I only know this code which are used after displayed in gridview
i use this to select one checkbox to isselected column how i convert this to multi checkboxlist to single column i use many ways more than 3 days without success
private void BindCheckBoxList() { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); string sqlStatement = "SELECT * FROM boby"; SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { CheckBoxList1.RepeatColumns = 4; // set the number of columns in the CheckBoxList CheckBoxList1.DataSource = dt; CheckBoxList1.DataTextField = "Name"; // the items to be displayed in the list items CheckBoxList1.DataValueField = "Name"; // the id of the items displayed CheckBoxList1.DataBind(); //Setting the Selected Items in the ChecBoxList based from the value in the database //to do this, lets iterate to each items in the list for (int i = 0; i < dt.Rows.Count; i++) { if (!string.IsNullOrEmpty(dt.Rows[i]["IsSelected"].ToString())) { CheckBoxList1.Items[i].Selected = Convert.ToBoolean(dt.Rows[i]["IsSelected"]); } } } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } private void Update(string name, bool isSelected) { SqlConnection connection = new SqlConnection(GetConnectionString()); SqlCommand cmd; string sqlStatement = string.Empty; try { connection.Open(); sqlStatement = "UPDATE handymen SET IsSelected = @IsSelected WHERE Name = @BizName"; cmd = new SqlCommand(sqlStatement, connection); cmd.Parameters.AddWithValue("@Name", name); cmd.Parameters.AddWithValue("@IsSelected", isSelected); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert/Update error"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindCheckBoxList(); } } protected void Button1_Click(object sender, EventArgs e) { string str = string.Empty; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { str = CheckBoxList1.Items[i].Text; Update(str, CheckBoxList1.Items[i].Selected); } } //ReBind the List to retain the selected items on postbacks BindCheckBoxList(); }
I have a stored procedure that returns 3 tables within the single recordset it returns. If I set a SqlDataSource to get data from this procedure, it works, but it only returns the first table. I want to have a GridView display the data from the 2nd or 3rd table using a SqlDataSource, but I can't figure out how to specify a particular table.
While there are some posts discussing this out there, I can't get a definitive answer about whether or not this is even possible (?). In a worst-case scenario, I can create another SP that only returns the 3rd table - but that creates a little maintenance headache that I would prefer to avoid.
So I found an example on how to search gridview using filterexpressions on this site (Filter GridView with TextBox using FilterExpression in SqlDataSource in ASP.Net), but now if I try to search with an apostrophe in the textbox, the code fails.
Here's the link to the the article/source - [URL] .....
How do I bind data from an Excel file named Book1.xlsx in Column A to a dropdownlist? I've tried like this:
Code: connExcel.Open() cmdExcel.CommandText = "SELECT distinct BCName From [Sheet1$] where BCName <> '' and ProjectName like 'Total%';" oda.SelectCommand = cmdExcel oda.Fill(dt2) connExcel.Close() dropdown1.DataSource = dt2 dropdown1.DataBind()
But the dropdown is filled with "System.Data.DataRowView" for all the 5 lines in the dropdown. How do I get the values shown instead of this? It must be some formatting of dt2 I think.
I have a GridView and a linqdatasource. The GridView is editable and when the user clicks to edit a row I want to concatenate two of the fields in the linqdatasource and place it in a single textbox.
I have a requirement to create multiple hyperlinks dynamically in a single GridView Column. The number of hyperlinks are not not known in advance and will depend on the results brought up by a query. How do I create the same?
Select tbl_name.name,tbl_midname.midname,tbl_last.lastname As name From tbl_name inner join tbl_midname on tbl_midname.id=tbl_name.id inner join tbl_last on tbl_last.id=tbl_name.id where tbl_name.id='1'
I have a gridview with textbox and that textbox is for searching. My question is do you know how to search with all fields in gridview.For example: I have a gridview contains, FIRST NAME, MIDDLE NAME, and LAST NAME, and if I type in the textbox the gridview will automatically get what I've inout in textbox.
I placed an Excel image in a header column on a Gridview. I want to allow the user the ability to click the icon and export the content of the Gridview to an Excel spreadsheet. How do I make the image an active link, so it will fire an event to access my code for the export.
I would like to transform the data in the column into hyper link. Let's say if i have "apple" in my column , when clicked on it i wud like it to go to "microsoft.com/apple".
Currently I have a grid view with few rows of data & edit and update for each row but I want it to be done on a single click with w external edit & save button how can the functionality be performed.
I have one filed that is encrypted using Rajindal algorithm and stored in MSSQL db.When I retrieve that column in gridview it's displayed in encrypted. I have the code that decrypts it but the problem is:How can I all loop throw this column values in the gridview and decrypt them all and display it in the gridview decrypted.See my code below:
display comma separated database values in a gridview? I'm not sure if I should handle this in a stored procedure or VB.net. I have a bunch of values stored in a checkbox list and the following database design: Each column datatype is a bit.
Val1 Val2 Val3 Val4 a b c d
I need something like this to be displayed in the gridview: a,b,c - depending on what values are set to 1 in the database.
I read some articles before posting to try and solve my issue but i didn't get anywhere,
Here is what i'm trying to do:
1) I have an excel sheet with 1 EntrantID column and the rest are numbers (these numbers refer to answer codes for certain questions),
So for example, my excel sheet looks something like:
ID --- a1 --- a2 --- a3 --- a4 --- a5 --- etc...
1 ---- 32 --- 55 --- 12 --- 121 -- 50---etc...
The goal is to traverse thru every row, and every column and according to a checking condition for the values of answers, i will add the number to one of two columns in another table, so for example, i will start traversing a1, i will check it's value which is 32, if 32 > 0 and 32 < 60, then i will add 32 to columnA in another table, else, i will add it to column B in that table, then i will move on to the next value, a2 which is 55, it's < 60 so i will 'add' it to that same columnA in that table..
At the end, my other table, the one with results should look like this:
PersonID --- columnA --- columnB
1 ------------ 32, 55, 12 --- 121, 50
Hope i didn't confuse things, i'm trying to explain as best as i can lol.
So what i did, i imported the excel sheet to sql server, and i looked up SQL language to see if i can do that thru SQL itself, and i couldn't find a way to traverse columns, so next, i started an ASP.NET project, connected to the DB and i drag/dropped the table in my page as a GridView, where i'm trying to traverse it so i can get the values, check them against the range, and insert them in relative column.
I tried something like this:
DataTable dt = new DataTable(); dt = (DataTable)GridView1.DataSource; DataTable temp = new DataTable("MyTable"); for (int i=0; i < dt.Rows.Count-1; i++) for (int k = 0; k < dt.Columns.Count-1; k++) if ((int)dt.Rows[i][k] > 0 && (int)dt.Rows[i][k] < 50) temp.Rows[i][k] = dt.Rows[i][k];
That is not working, and i'm unable to do it. basically, i don't know how to fetch the value inside each [row][column], and overall i don't know how i can do the required task.
I have a parts table (ID, PartName, Category). I have an Engine table that has 10 columns to specify the 10 main parts that comprise the engine (EngineID, PartA_ID, PartB_ID, PartC_ID...).
I want to make only one trip to the DB to pull all the rows, then filter by category for each PartA, PartB, etc ddl. how to do this?
I'm attempting to hook up multiple DropDownLists to a single SQLDataSource, but I want the SQLDataSource to return different rows depending on which DropDownList it is populating.
Say I have a table full of names (we'll call it TABLE_NAMES):
[code]....
....and I have two dropdown lists, one with a list of person names and one with a list of dog names.
Is there a way to link up each of these dropdown lists to a single SQLDataSource and use the SelectParameter to differentiate between which records I want returned for each dropdown list?
To make matters worse, these dropdown lists will appear within a GridView control....so there will be multiple instances of each dropdown list depending on how many rows there are in the GridView.