Solved

insertcommand in a grid getting the name of a dropdown

Posted on 2013-01-23
8
313 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

911 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

17 Experts available now in Live!

Get 1:1 Help Now