How To save selected value from a treeview into database

Posted by Bhupender.Yadav under ASP.NET on 4/22/2013 | Points: 10 | Views : 4646 | Status : [Member] | Replies : 12
hey friends,
Can anyone help me in ,how to save the selected value from a treeview into database

With Regards,
Bhupender Yadav



Responses

Posted by: Prabhukiran345 on: 4/22/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
Use this Script code:

<script language="javascript">
function TVIndexChanged()
{
ChangeText( 'node changed' );
}
function TVNodeExpand()
{
ChangeText( 'onexpand' );
}
function TVNodeCollapse()
{
ChangeText( 'oncollapse' );
}

function TVDoubleClick()
{
ChangeText( 'dblclick' );
}
function TVRightClick()
{
var tree = GetTreeHandle();
var treenode;
if ( null == tree || undefined == tree )
return;
treenode = tree.getTreeNode( event.treeNodeIndex );
if ( null == treenode || undefined == treenode )
return;

// Cause the tree node that was
// right-clicked on to become the
// selected node.
tree.selectedNodeIndex = event.treeNodeIndex;

ChangeText( 'oncontextmenu' );
}

// Simply changes the information
// in the display text boxes to
// demonstrate how to obtain meta-data
// from the selected node's
// NodeData property on the client.
//
function ChangeText( eventName )
{
var treeNode = GetSelectedNode();

if ( null == treeNode || undefined == treeNode )
{
return;
}

var nodeData =
treeNode.getAttribute( 'nodeData' ).split( ';' );

var id = GetKeyValue( 'SomeId' );
var name = GetKeyValue( 'Name' );

document.getElementById( 'txtEvent' ).value =
eventName;
document.getElementById( 'txtId' ).value = id;
document.getElementById( 'txtName' ).value = name;
}

// Gets the value of the searchKey
// from the NodeData of a TreeNode.
//
function GetKeyValue( searchKey )
{
// Get a handle to the selected TreeNode.
var treenode = GetSelectedNode();

// Validate the node handle.
if ( null == treenode || undefined == treenode )
return null;

// Get the node's NodeData property's value.
var nodeDataAry = treenode.getAttribute( 'nodeData' );

if ( null == nodeDataAry || undefined == nodeDataAry )
return null;

nodeDataAry = nodeDataAry.split( ';' );

if ( null == nodeDataAry || undefined == nodeDataAry ||
0 >= nodeDataAry.length )
return null;

var count = 0;
var returnValue = null;

while ( count < nodeDataAry.length )
{
var workingItem = nodeDataAry[ count ];

if ( 0 >= workingItem.length )
{
count++;
continue;
}

// Split the string into its key value pairs.
var kv = workingItem.split( '=' );

if ( 1 >= kv.length )
{
count++;
continue;
}

var key = kv[ 0 ];
var kValue = kv[ 1 ];

if ( key != searchKey )
{
count++;
continue;
}

returnValue = kValue;
break;
}

return returnValue;
}

// Gets a handle to the TreeView.
//
function GetTreeHandle()
{
var tree;
var treeName = 'tvControl';

// Get a handle to the TreeView.
tree = document.getElementById( treeName );

if ( null == tree || undefined == tree )
return null;

return tree;
}

// Gets a handle to the TreeView's selected node.
//
function GetSelectedNode()
{
var tree = GetTreeHandle();
var treeNode;

if ( null == tree || undefined == tree )
return null;

treeNode = tree.getTreeNode( tree.selectedNodeIndex );

if ( null == treeNode || undefined == treeNode )
return null;

return treeNode;
}
</script>

You may refer this link for complete example:http://www.codeproject.com/Articles/10722/ASP-NET-TreeView-Control-the-Client-s-Browser

Thanks,
Prabhu Kiran Bommareddy

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/22/2013 [Member] Starter | Points: 25

Up
0
Down
hi Prabhu Kiran,
Thanks For Your Response.
But Can You tell me How to insert the record of treeview into database using entity framwork.

With Regards,
Bhupender Yadav

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhukiran345 on: 4/22/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Bhupendra,
Here i have taken static tree view,and u can also create it dynamically if u want.

ASPX CODE:

<asp:TreeView ID="TreeView1" runat="server" onselectednodechanged="TreeView1_SelectedNodeChanged">
<Nodes>
<asp:TreeNode Value="Parent1" Expanded="True" Text="1">
<asp:TreeNode Value="Child1A" Text="A" />
<asp:TreeNode Value="Child1B" Text="B" />
</asp:TreeNode>
<asp:TreeNode Value="Parent2" Text="2"></asp:TreeNode>
<asp:TreeNode Value="Parent3" Expanded="True" Text="3">
<asp:TreeNode Value="Child3A" Text="A"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>

CS CODE:
// cODE FOR Selected node changed event
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
string s = TreeView1.SelectedNode.Value;//To get value of treeview
EntityName objEntity = new EntityName ();//create object to entity
objEntity .tableName.AddObject(new tableName() {Node= s });//Inserting into Treeview
objEntity .SaveChanges();
}

Please mark as answer if the solution seems to be helpful.

Thanks & Regards,
PRABHU KIRAN.B

Thanks,
Prabhu Kiran Bommareddy

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/22/2013 [Member] Starter | Points: 25

Up
0
Down
Here is my code,please solve it,i am getting an error while saving data.
error: Object reference not set to an instance of an object

if (TreeView1.CheckedNodes.Count > 0)
{


foreach (TreeNode tnode in TreeView1.CheckedNodes)
{
if (tnode.Checked==true)
{
//string str;
//str = tnode.Value;
string s = TreeView1.SelectedNode.Text;
UsrPro.fld_UserName = ddlUsername.SelectedItem.Text;
UsrPro.fld_ProjectName = s;
UsrPro.fld_Uid = Convert.ToInt32(ddlUsername.SelectedValue);
UsrPro.fld_ProId = Convert.ToInt32(TreeView1.SelectedNode.Selected);
UsrPro.fld_ProjectName = TreeView1.SelectedNode.Text;
//UsrPro.fld_CreatedBy =
//if (GvUserMappng.Rows.Count > 0)
//{
// UsrPro.fld_SubProId =Convert.ToInt32(TreeView1.SelectedNode.ChildNodes.ToString());
//}
ehour.UsrPromappings.AddObject(new UsrPromapping() { s});
ehour.AddToUsrPromappings(UsrPro);
ehour.SaveChanges();
gvusermapped.DataBind();
GvUserMappng.DataBind();
lblmsg.Text = "Record Saved Successfully";
ResetValues();
}
}
}

With Regards,
Bhupender Yadav

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhukiran345 on: 4/22/2013 [Member] Starter | Points: 25

Up
0
Down
HI,

By analysing your code i think so you went wrong in declaring object "UsrPro".... And you have added "s" in tghe code ehour.UsrPromappings.AddObject(new UsrPromapping() { s });... So please remove it.

Thanks,
Prabhu Kiran Bommareddy

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
hi,
Here UsrPromappings is the entity set name.if i change it to UsrPromapping it show that the it does not contain defination for it.

With Regards,
Bhupender Yadav

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhukiran345 on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
If possible once send your application as attachment. Will solve and send u back.

Thanks,
PRABHU KIRAN.B

Thanks,
Prabhu Kiran Bommareddy

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
Hi
Here Is the attached File.
 Download source file

With Regards,
Bhupender Yadav

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhukiran345 on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Bhupender,
Once checkout with replacing with this code.. If u again raise any exception,Please reply me back with line of code you are getting the exception. I have to do all the DB connections inorder to check in my system.

if (TreeView1.CheckedNodes.Count > 0)
{
foreach (TreeNode tnode in TreeView1.Nodes)
{
if (tnode.Checked == true)
{
//string str;
//str = tnode.Value;
string s = TreeView1.SelectedNode.Text;
UsrPro.fld_UserName = ddlUsername.SelectedItem.Text;
UsrPro.fld_ProjectName = s;
UsrPro.fld_Uid = Convert.ToInt32(ddlUsername.SelectedValue);
UsrPro.fld_ProId = Convert.ToInt32(TreeView1.SelectedNode.Selected);
UsrPro.fld_ProjectName = TreeView1.SelectedNode.Text;
ehour.UsrPromappings.AddObject(UsrPro);
ehour.SaveChanges();
gvusermapped.DataBind();
GvUserMappng.DataBind();
lblmsg.Text = "Record Saved Successfully";
ResetValues();
}
}
}

Thanks,
PRABHU KIRAN B

Thanks,
Prabhu Kiran Bommareddy

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Prabhu Kiran,
The above code is not working.
error:-Object reference not set to an instance of an object

and after solving that according to your suggestion as by removing "s"
it show that the it does not contain defination for it.

With Regards,
Bhupender Yadav

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhukiran345 on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

may I know at which line of code, the error:-Object reference not set to an instance of an object ,has encountered.
reply me back that line of code...

Thanks
Prabhu


Thanks,
Prabhu Kiran Bommareddy

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
hi sir
i am getting this error on treeview.selectedvalue

With Regards,
Bhupender Yadav

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response