Solved

insertcommand in a grid getting the name of a dropdown

Posted on 2013-01-23
8
319 Views
Last Modified: 2013-01-28
I am trying to insert the selected value of a dropdown box into the database and I cant get it - anyone know?

            string sqlStr = "";
            int mgrid = user.UserID;
            int selecteduser;
            Control myControl = FindControl("ddlusers");
            if (myControl != null || Convert.ToString(myControl) != "--Please Select--")
            {

                selecteduser = myControl.SelectedValue; <-- selected value doesnot exist 
                
            }
            sqlStr += "Insert Into ManagerStaff (mgruseruid,staffuseruid) Values (mgrid,selecteduser) ";
            using (SqlConnection conn = new SqlConnection(conStr))
            using (SqlCommand cmd = new SqlCommand(sqlStr, conn))
            {
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }

Open in new window

0
Comment
Question by:r3nder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:psgindiana
ID: 38812092
Error messages, or just no results?
0
 
LVL 6

Author Comment

by:r3nder
ID: 38812103
'System.Web.UI.Control' does not contain a definition for 'SelectedValue' and no extension method 'SelectedValue' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)
0
 
LVL 9

Expert Comment

by:psgindiana
ID: 38812145
Try
selecteduser = ((DropDownList)myControl).SelectedValue.ToString();

Open in new window

This will cast the control as a DropDownList, as well as return a string you can use.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:r3nder
ID: 38812192
"Object reference not set to an instance of an object."
0
 
LVL 6

Author Comment

by:r3nder
ID: 38812199
here is the asp
 <Columns>
                     <telerik:GridDropDownColumn UniqueName="ddlusers" DataSourceID="UserDD" ListValueField="useruid" ListTextField="username" 
                      HeaderText="Users" Visible="false">
                     </telerik:GridDropDownColumn>

Open in new window

0
 
LVL 9

Assisted Solution

by:psgindiana
psgindiana earned 500 total points
ID: 38812258
On what line? If you debug, does it even get to line 8 of your original post?

This is not a "DropDownList" it is a telerik specific control called "GridDropDownColumn". You can try replacing "DropDownList" with "GridDropDownColumn" in the cast. Not sure that's your biggest problem, though. Still not sure if your findcontrol is even returning a control.
0
 
LVL 6

Accepted Solution

by:
r3nder earned 0 total points
ID: 38812330
GridEditableItem item = e.Item as GridEditableItem;

                selecteduser = Convert.ToInt32((item["ddlusers"].Controls[0] as RadComboBox).SelectedValue);
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38826198
Thanks for the info - with your comment I was able to figure it out psgindiana
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
batch file or script 4 47
ModalPopup  question 22 38
How to force output to ascii 2 34
orderby list (from Json) 1 28
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this post we will learn different types of Android Layout and some basics of an Android App.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question