Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: ListBox in DataGrid!


Message #1 by "yanpeng" <yanpeng_512@h...> on Mon, 24 Feb 2003 07:14:14
I am a aspx beginner!I meet a trouble with listbox!I applicated it in 
EditItemTemplate of DataGrid,and I want to insert the choosed-items into 
database,but the system alerted that "Object reference not set to an 
instance of an object.",so I need get some help here:).Thanks for any 
reply!
My code is behind:
aspx code--
<asp:TemplateColumn
			HeaderText="Privi">
			
			<ItemTemplate>
			</ItemTemplate>
			<EditItemTemplate>
			<asp:ListBox id="ChangePriviSelect" 
datasource='<%#GetPrivi()%>' 
			    DataTextField="PriviName"        
			    DataValueField="PriviId"
                            runat=server size="6" SelectionMode=Multiple >
				</asp:ListBox> 

			</EditItemTemplate>
		</asp:TemplateColumn>

C# code--
protected void MyDataGrid_Update(object obj,DataGridCommandEventArgs e)
         {
          string IdNum1;
	 Random RandomGen=new Random();
          string ChangeConnectionString="server=\'ntserver\'; user 
id=\'cnacc\'; password=\'cnacc\'; Database=\'cnacc\'";
	 string Name=((TextBox)e.Item.Cells[1].Controls[0]).Text;
	 string RoleId=MyDataGrid.DataKeys[(int)
e.Item.ItemIndex].ToString(); 

           for (int i=0;i<ChangePriviSelect.Items.Count;i++)
	    {
	      if(ChangePriviSelect.Items[i].Selected)
	       {
	         IdNum1=RandomGen.Next(0,50000).ToString()+RandomGen.Next
(50000,100000).ToString()+RandomGen.Next(100000,999999).ToString();
                 insertprivi="INSERT INTO T_Role (RoleId,RoleName,PriviId) 
VALUES ('"+IdNum1+"','"+RoleId+"','"+ChangePriviSelect.Items
[i].Value+"')"; 
                 SqlConnection InsertPriviConn=new SqlConnection
(ChangeConnectionString);
                 SqlCommand InsertPriviCmd=new SqlCommand
(insertprivi,InsertPriviConn);
	         InsertPriviConn.Open();
	         InsertPriviCmd.ExecuteNonQuery();
	         InsertPriviConn.Close();
               }
	    }
	   
	   MyDataGrid.EditItemIndex  =  -1;   
	   BindGrid();
	 }


waiting for your answer!:)Thanks a lot!
Message #2 by =?gb2312?B?0dUgxfQ=?= <yanpeng_512@h...> on Mon, 24 Feb 2003 07:02:24 +0000




>I am a aspx beginner!I meet a trouble with listbox!I applicated it in
>EditItemTemplate of DataGrid,and I want to insert the choosed-items into
>database,but the system alerted that "Object reference not set to an
>instance of an object.",so I need get some help here:).Thanks for any
>reply!
>My code is behind:
>aspx code--
><asp:TemplateColumn
>			HeaderText="Privi">
>
>			<ItemTemplate>
>			</ItemTemplate>
>			<EditItemTemplate>
>			<asp:ListBox id="ChangePriviSelect" datasource='<%#GetPrivi()%>'
>			    DataTextField="PriviName"
>			    DataValueField="PriviId"
>                             runat=server size="6" SelectionMode=Multiple 
>
>				</asp:ListBox>
>
>			</EditItemTemplate>
>		</asp:TemplateColumn>
>
>C# code--
>protected void MyDataGrid_Update(object obj,DataGridCommandEventArgs e)
>          {
>           string IdNum1;
>	 Random RandomGen=new Random();
>           string ChangeConnectionString="server=\'ntserver\'; user
>id=\'cnacc\'; password=\'cnacc\'; Database=\'cnacc\'";
>	 string Name=((TextBox)e.Item.Cells[1].Controls[0]).Text;
>	 string RoleId=MyDataGrid.DataKeys[(int)e.Item.ItemIndex].ToString();
>
>            for (int i=0;i<ChangePriviSelect.Items.Count;i++)
>	    {
>	      if(ChangePriviSelect.Items[i].Selected)
>	       {
>
>IdNum1=RandomGen.Next(0,50000).ToString()+RandomGen.Next(50000,100000).ToString()+RandomGen.Next(100000,999999).ToString();

>
>                  insertprivi="INSERT INTO T_Role 
(RoleId,RoleName,PriviId)
>VALUES 
('"+IdNum1+"','"+RoleId+"','"+ChangePriviSelect.Items[i].Value+"')";
>
>                  SqlConnection InsertPriviConn=new
>SqlConnection(ChangeConnectionString);
>                  SqlCommand InsertPriviCmd=new
>SqlCommand(insertprivi,InsertPriviConn);
>	         InsertPriviConn.Open();
>	         InsertPriviCmd.ExecuteNonQuery();
>	         InsertPriviConn.Close();
>                }
>	    }
>
>	   MyDataGrid.EditItemIndex  =  -1;
>	   BindGrid();
>	 }
>
>
>waiting for your answer!:)Thanks a lot!
>
>
>
>
>_________________________________________________________________
>与联机的朋友进行交流,请使用 MSN Messenger:  http://messenger.msn.com/cn


_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger:  http://messenger.msn.com/cn  


  Return to Index