Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insertcommand in a grid getting the name of a dropdown

Posted on 2013-01-23
8
Medium Priority
?
324 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
  • 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 1500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

971 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