xml format in asp.net - asp.net

I have a table in sqlserver, in that table i have a column having xml file. i am displaying that table in gridview. when we click on button i want to display that particular xml column file column in a xml format. tank you.
i take one dtalist and writ code like this.
<asp:DataList ID="dtlstOne" runat ="server" >
<ItemTemplate >
<asp:TextBox ID="txtname" runat ="server" Text ='<%# XPath("/EXTPOSTTRANSACTION/STLTRANSACTIONINFO/TRANSACTIONTYPE")%>' ></asp:TextBox>
<asp:TextBox ID="TextBox1" runat ="server" Text ='<%# XPath("/EXTPOSTTRANSACTION/STLTRANSACTIONINFO/USERID")%>' ></asp:TextBox>
</ItemTemplate>
</asp:DataList>
it is giving error
An exception of type 'System.ArgumentException' occurred in System.Web.dll but was not handled in user code
Additional information: XPath DataBinding: 'System.Data.DataRowView' must implement IXPathNavigable.
please can u help me
string sqry = "select NewLoanRequestXML from tbl_newloanxml where newloanid=" + Request.QueryString["id"].ToString();
da = new SqlDataAdapter(sqry, con);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
dtlstOne.DataSource = ds;
dtlstOne.DataBind();
}

Related

Add dynamic radio button object to GridView

I need to add a custom radio button control that I created based on an if condition to my GridView. My radiobutton will be enabled or disabled based on this condition and will have the text
changed as well.
I'm trying to figure out how to add a radiobutton object into my data row instead of a string dt.Columns.Add("FirstName").
<telerik:RadGrid runat="server" ID="grd1" OnNeedDataSource="grd1_NeedDataSource">
<MasterTableView AutoGenerateColumns="False">
<Columns>
<telerik:GridTemplateColumn HeaderText="Radiobutton header" UniqueName="col1">
<ItemTemplate>
<asp:RadioButton ID="rbType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "rbEnableorDisable")%>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="FirstName header" UniqueName="col2">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
CodeBehind
Private dt As DataTable
Private dr As DataRow
dt= New DataTable
dt.Columns.Add("rbEnableorDisable")
dt.Columns.Add("FirstName")
Dim rb As RadioButton
rb = New RadioButton
For each item in itemlist //some data iteration declared elsewhere
dr = dt.NewRow()
If (Condition)
rb.Text = "Should be Disabled"
rb.Enabled = False
Else
rb.Text = "Should be Enabled"
rb.Enabled = True
End if
dr.Item("FirstName") = item.FirstName
dr.Item("rbEnableOrDisable") = rb//?Code for inserting a radio button object
dt.Rows.Add(dr)
Next
With grd1
.DataSource = dt
.DataBind()
End With
So far with this code
I am only able to display the radiobutton text if i have dr.Item("rbEnableOrDisable") = rb.Text.
I need to display the whole radiobutton object(show the text and if it's enabled or disabled among others)
I tried
LocationData.Columns.Add(New DataColumn("rbType", GetType(RadioButton)))
but it seems I need to append to the ItemTemplate
Also tried adding the whole column dynamic with:
grd1.Controls.Add(rb)
You need to have something in your DataItem to put the radiobutton enabled or disabled and assigment to Enabled property.
Enabled='<%# DataBinder.Eval(Container.DataItem, "booleanData")%>'
If you need to put RadioButton checked or unchecked use Checked property.

losing the first result of retrieved data when binding it to repeater control

I am developing a social network in which i must show a list of friends to the current user.
i am using SqlDataReader to retrieve data from database then bind it by a repeater , the problem is that the repeater always skip the first result so it only shows the n-1 results out of n. anybody can explain to me this behaviour?
my code is:
string cmdstr2 = "SELECT students.fname, students.lname,students.username FROM students INNER JOIN friends ON students.username = friends.tostudent WHERE (friends.fromstudent ='" + cuser + "')";
SqlCommand cmd2 = new SqlCommand(cmdstr2, sc);
SqlDataReader rd = cmd2.ExecuteReader();
if (rd.Read())
{
Repeater1.DataSource = rd;
Repeater1.DataBind();
}
in the design view, i have written this code to include the repeater in the page:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<div style="font-size:xx-large;">
الأصدقاء</div>
</HeaderTemplate>
<ItemTemplate>
<div style="font-size:x-large; color:Black; margin-right:0px; margin-top:0px;">
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "student.aspx?user="+DataBinder.Eval(Container.DataItem,"username")%>' >
<%#DataBinder.Eval(Container.DataItem, "fname")%> <%#DataBinder.Eval(Container.DataItem, "lname")%>
</asp:HyperLink>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
Your rd.Read() in your if statement is advancing it 1 record
Try using:
if (rd.HasRows)

How to Redirect to new asp page while clicking on the data row of the data grid view in asp.net

My data grid view in asp.net displays only selected id from sql server database as a single row.
Now I need to display a particular page when I click on the particular id (data row) and also I want to display all the details in a new page belongs to the selected id (on click on the id).
I have tried this code:
protected void btnserch_Click(object sender, EventArgs e)
{
SqlConnection objsqlconnection = new SqlConnection(CONNECTIONSTRING);
string query = "Select id from registration";
SqlCommand objsqlcommand = new SqlCommand(query, objsqlconnection);
objsqlconnection.Open();
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
objsqlcommand.CommandText = query;
objsqlcommand.Connection = objsqlconnection;
da = new SqlDataAdapter(objsqlcommand);
da.Fill(ds);
objsqlcommand.ExecuteNonQuery();
GridView1.DataSource = ds;
GridView1.DataBind();
objsqlconnection.Close();
}
This code returns a grid by selecting only id column from registration table. Now when I click on the data row of the id column, I need an another page which should display all the details belonging to that id.
like #MelanciaUK said "OnItemDataBound" is your friend.
For checking ID even on postback, select it in "SelectedIndexChanging"
hope it helps.
Easiest way to redirect to new page:
<asp:GridView ID="grd" runat="server" autogeneratedcolumn="false">
<asp:TemplateField HaderText="ID">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HaderText="Name">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HaderText="Edit">
<ItemTemplate>
<a href='Newpage.aspx?ID=<%#Eval("ID")%>'>Edit</a>
<ItemTemplate>
<asp:TemplateField>
<asp:GridView>
You can redirect from one page another page in two ways
<asp:GridView ID="grd" runat="server" autogeneratedcolumn="false">
<asp:TemplateField HaderText="Action">
<ItemTemplate>
<a href='mypage.aspx?ID=<%#Eval("RowID")%>'>Edit</a>
Edit
<ItemTemplate>
<asp:TemplateField>
<asp:GridView>
<script type="text/javascript">
function Redirect(id) {
window.location = 'mypage.aspx?ID=' + id;
}
</script>

Binding data to Gridview Header Template? In Asp.net

In web application, I am trying to bind the Header Template of the gridivew, but i am not able to bind the data to gridview header.
<asp:GridView ID ="grdInner" runat ="server" AutoGenerateColumns ="false" >
<Columns >
<asp:TemplateField >
<HeaderTemplate >
<asp:Label ID="lblHeader" runat="server" Text='<%# Eval("title") %>'></asp:Label>
</HeaderTemplate>
<ItemTemplate >
<asp:Label ID ="lblDesc" runat ="server" Text ='<%# Eval("description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Following code achieves the same things
<asp:GridView runat="server" ID="gridView" onrowdatabound="gridView_OnRowDataBound" AutoGenerateColumns="false">
<columns>
<asp:TemplateField>
<HeaderTemplate><asp:Label runat="server" ID="lblHeader"></asp:Label></HeaderTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
You can set label text in OnRowDataBound event
protected void gridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
((Label)e.Row.FindControl("lblHeader") as Label).Text = "Your Data here";
}
Another way to do this that is fairly simple if you want to bind the DataTable column headers to the Gridview headers (for example, you are generating DataTables where the column names can be different and you want these dynamic names to be the GridView header column names).
DataTable dt = // Call your DataTable here.
// Get your column names here.
string nutrient = dt.Columns[2].ColumnName;
string nutrient1 = dt.Columns[3].ColumnName;
string nutrient2 = dt.Columns[4].ColumnName;
string nutrient3 = dt.Columns[5].ColumnName;
string nutrient4 = dt.Columns[6].ColumnName;
string nutrient5 = dt.Columns[7].ColumnName;
GridView1.DataSource = dt;
GridView1.DataBind();
if (GridView1.Rows.Count > 0)
{
GridView1.HeaderRow.Cells[2].Text = nutrient;
GridView1.HeaderRow.Cells[3].Text = nutrient1;
GridView1.HeaderRow.Cells[4].Text = nutrient2;
GridView1.HeaderRow.Cells[5].Text = nutrient3;
GridView1.HeaderRow.Cells[6].Text = nutrient4;
GridView1.HeaderRow.Cells[7].Text = nutrient5;
}
You can also do the same thing with the footer row using GridView1.FooterRow.Cells[x].Text = ...

Spliting data in Asp.Net Gridview

I Have a set of choices(options) coming from Database(around 36 records) which I have to insert in Gridview in such a way that 18 will go one column and 18 in another. And As these are choices the columns are needs to be a check box columns. So What I did is I have created a data table with 2 columns and split the data accordingly and then bind it to the grid.But the problem is If the question list is having odd no of items count ie 37. It is adding a blank record with check box in my gridview. Your help will be appreciated...
see code below
In my Aspx.cs
DataTable dTable = new DataTable();
dTable.Columns.Add("Questionsclmn1", typeof(string));
dTable.Columns.Add("Questionsclmn2", typeof(string));
for (int item = 0; item < QuestionList.Count; item = item + 2)
{
DataRow drow = dTable.NewRow();
drow["Questionsclmn1"] = QuestionList[item].Question;
if ((item + 1) < QuestionList.Count)
drow["Questionsclmn2"] = QuestionList[item + 1].Question;
dTable.Rows.Add(drow);
}
GrdVwQuestionsList.DataSource = dTable;
GrdVwQuestionsList.DataBind();
In my Aspx file under gridview
<Columns>
<asp:TemplateField HeaderText="Please Choose the Options Below">
<ItemTemplate>
<asp:CheckBox ID="chkQuestionList1" runat="server"
Text='<%# DataBinder.Eval( Container.DataItem, "Questionsclmn1")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkQuestionList2" runat="server"
Text='<%# DataBinder.Eval( Container.DataItem, "Questionsclmn2")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Thanks in Advance.
Regards,
Chetan
I wouldn't use a GridView for this at all. CheckboxList can lay out checkboxes in two columns automatically.
<asp:CheckboxList runat="server" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Vertical" ... />
There's also DataList that supports the same properties but lets you use a template for the content.

Resources