Solved

insertcommand in a grid getting the name of a dropdown

Posted on 2013-01-23
8
312 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
 
LVL 6

Author Comment

by:r3nder
ID: 38812192
"Object reference not set to an instance of an object."
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now