Hi
I have used the dataSource Manually(Connecting in Properties) as well as in coding ..
I am working on RadGrid CRUD operation,While connecting the database in code it shows object refernce is not set.
What I have tried:
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Telerik.Web.UI;
namespace Telerik08._03
{
public partial class Sample123 : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private static void ShowCheckedItems(Telerik.Web.UI.RadComboBox comboBox, Literal literal)
{
}
protected void RadComboBox1_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void RadComboBox2_DataBound(object sender, EventArgs e)
{
}
protected void RadComboBox2_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
}
protected void RadComboBox2_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
{
}
protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
{
}
protected void bind()
{
cn.Open();
SqlCommand cmd = new SqlCommand("Select * from gvdetails17", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
RadGrid1.DataSource = ds;
RadGrid1.DataBind();
}
}
protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
GridDataItem item = (GridDataItem)e.Item;
string EmployeeID = item.OwnerTableView.DataKeyValues[item.ItemIndex]["EmpID"].ToString();
try
{
cn.Open();
SqlCommand cmd = new SqlCommand("delete from gvdetails17 where EmpId=" + EmployeeID, cn);
int result = cmd.ExecuteNonQuery();
cn.Close();
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to delete Employee. Reason: " + ex.Message));
e.Canceled = true;
}
}
public RadComboBoxItem item { get; set; }
public RadComboBoxItem EmpName { get; set; }
protected void RadComboBox2_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
}
My aspx page
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample123.aspx.cs" Inherits="Telerik08._03.Sample123" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.col1,
.col2,
.col3 {
margin: 0;
padding: 0 5px 0 0;
width: 30%;
line-height: 14px;
float: left;
}
.multipleRowsColumns .rcbItem,
.multipleRowsColumns .rcbHovered {
float: left;
margin: 0 1px;
min-height: 13px;
overflow: hidden;
padding: 2px 19px 2px 6px;
width: 193px;
}
.exampleRadComboBox.RadComboBoxDropDown .rcbHeader {
padding: 5px 27px 4px 7px;
}
.results {
display: block;
margin-top: 20px;
}
.RadDock a {
color: #58954D;
}
.alternateItemStyle
{
background-color: #FFFFFF;
color: #000000;
font-size: 8pt;
}
.higherZIndex {
z-index: 2;
}
.demo-container.wrapper {
background-image: url(images/background.png);
background-repeat: no-repeat;
width: 260px;
padding: 190px 30px 10px;
margin: 0 auto;
border: 0 none;
}
.demo-container > .RadInput,
.demo-container .RadPicker,
.button-wrapper .RadButton {
width: 100%;
margin-bottom: 10px;
}
html .rbCheckBox {
vertical-align: middle;
}
.button-wrapper {
padding: 25px 13px 0;
}
.background-material .rbCheckBox,
.background-material .RadLabel {
margin-left: 13px;
margin-right: -13px;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
<%-- $(function () {
var grid = document.getElementById('<%= RadGrid1.ClientID%>');
$('tr', grid).on('dblclick', function (obj, b) {
$("#id").html($(".clsempid", $(this).closest("tr")).html());
$("#dialog").dialog({
title: "View Details",
buttons: {
Ok: function () {
$(this).dialog('close');
}
},
modal: true
});
})
});--%>
</script>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="Button1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="Label1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<table style="margin-left:471px;margin-bottom:-27px">
<tr>
<td>
<telerik:RadComboBox RenderMode="Classic" ID="RadComboBox1" Text="Select" runat="server"
CheckBoxes="true" EnableCheckAllItemsCheckBox="true" MarkFirstMatch="true" HighlightTemplatedItems="true"
AutoPostBack="true" ValueField="--Select--" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged"
> <%----%>
<Items>
<telerik:RadComboBoxItem Text="Dotnet" />
<telerik:RadComboBoxItem Text="Java" />
<telerik:RadComboBoxItem Text="Sales" />
<telerik:RadComboBoxItem Text="Hr" />
<telerik:RadComboBoxItem Text="Admin" />
</Items>
</telerik:RadComboBox>
</td>
</tr>
</table>
<telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="RadComboBox2" style="margin-left:671px;margin-bottom:24px;width:250px"
DataSourceID="SqlDataSource1" AutoPostBack="true" Text="Select" MarkFirstMatch="true" EnableLoadOnDemand="true" HighlightTemplatedItems="true"
ValueField="--Select--"
OnDataBound="RadComboBox2_DataBound" OnItemDataBound="RadComboBox2_ItemDataBound"
OnSelectedIndexChanged="RadComboBox2_SelectedIndexChanged">
<HeaderTemplate>
<ul>
<li class="col1">Employee ID</li>
<li class="col2">Employee Name</li>
<li class="col3">Salary</li>
<li class="col3">Department</li>
<li class="col3">Age</li>
</ul>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td style="width: 225px">
<%#DataBinder.Eval(Container.DataItem,"EmpId") %>
</td>
<td style="width: 225px">
<%#DataBinder.Eval(Container.DataItem,"EmpName") %>
</td>
<td style="width: 225px">
<%#DataBinder.Eval(Container.DataItem,"Sal") %>
</td>
<td style="width: 225px">
<%#DataBinder.Eval(Container.DataItem,"Dep") %>
</td>
<td style="width: 225px">
<%#DataBinder.Eval(Container.DataItem,"Age") %>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
A total of
<asp:Literal runat="server" ID="RadComboItemsCount" />
items
</FooterTemplate>
</telerik:RadComboBox>
<telerik:RadButton RenderMode="Lightweight" runat="server" ID="Button1" Text="Select" OnClick="Button1_Click" ></telerik:RadButton>
<asp:Label runat="server" ID="Label1" />
<div style="margin-top:-46px;background-color:lightgreen; border-collapse: collapse;
background-color: lightgreen;">
<telerik:RadGrid ID="RadGrid1" ShowStatusBar="true" margin-top="-46px" Height="500px" runat="server"
ShowGroupPanel="True" GridLines="None"
AllowPaging="True" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
AllowFilteringByColumn="True" OnDeleteCommand="RadGrid1_DeleteCommand"
AllowSorting="True" AutoGenerateDeleteColumn="true"
AutoGenerateEditColumn="True" CellSpacing="0" DataSourceID="SqlDataSource1"
Skin="Forest" AllowCustomPaging="True" AllowMultiRowEdit="True" AllowMultiRowSelection="True" PageSize="20" >
<HeaderContextMenu>
<CollapseAnimation Type="OutQuint" Duration="200" />
</HeaderContextMenu>
<MasterTableView GroupLoadMode="Client" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<telerik:GridBoundColumn DataField="EmpId" DataType="System.Int32" FilterControlAltText="Filter EmpId column" HeaderText="EmployeeId" SortExpression="EmpId" UniqueName="EmpId">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="EmpName" FilterControlAltText="Filter EmpName column" HeaderText="EmployeeName" SortExpression="EmployeeName" UniqueName="EmpName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Dep" FilterControlAltText="Filter Dep column" HeaderText="Department" SortExpression="Dep" UniqueName="Department">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Age" FilterControlAltText="Filter Age column" HeaderText="Age" SortExpression="Age" UniqueName="Age">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Sal" DataType="System.Int32" FilterControlAltText="Filter Sal column" HeaderText="Salary" SortExpression="Salary" UniqueName="Sal">
</telerik:GridBoundColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
<%----%>
<FilterMenu>
</FilterMenu>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SST321ConnectionString %>" SelectCommand="SELECT EmpId,EmpName,Dep,Age,Sal FROM [gvdetails17]"></asp:SqlDataSource>
<asp:Literal ID ="itemsClientSide" runat="server" />
</div>
</form>
</body>
</html>