Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 109,547 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,175 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Issue with dropdownlist and sql query

 
Reply to this topicStart new topic

Issue with dropdownlist and sql query

Zehuti
post 6 Aug, 2008 - 06:45 PM
Post #1


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 6

Hello,

So I'm having a weird little issue.. I'm using a dropdown list in a ComponentArt Dialog (if you don't know what this is, it doesn't matter too much, just a popup menu) and within this dropdown list is a list of projects to select from.

PROBLEM SUMMARY: When I select an item from the dropdown list, and click the submit button, it registers a different value than the selected value from the dropdown list.

Now, when I submit this code, I want it to take the information from the popup (textbox info) and input it into the database with a projectid value of the DataValueField in the selected item from the dropdown list. This works great... the first time... if I open the popup on the first click (when visiting the site for one session) it submits everything perfectly when I click the submit button (this closes the popup, submits the info, and closes the popup) If I prompt the popup again, enter info, and hit submit, it closes, and then my error checking label tells me that it tried to take the DataValueField of the first item in the list, not the one that I selected. So somewhere I assume that it resets somehow.. I just don't understand why. Below is the function for the submit task button, as well as part of the buildgrid() function that manipulates the dropdown list.

Let me know if you need anything else.

Any ideas?

Thanks a lot in advance, been working on this for a few hours now..

csharp

OleDbConnection MyConnection = new OleDbConnection();
MyConnection = PLDBMgr.dbconnect.GetConn();

string percentComplete;

if (Int32.Parse(txtbTaskSlider.Text) < 10)
percentComplete = "0" + txtbTaskSlider.Text;
else
percentComplete = txtbTaskSlider.Text;

string userid = Session["s_curuserId"].ToString();
string associated_project = ddlProject.SelectedValue.ToString();
string name = txtbTaskName.Text;
string startdate = txtbTaskStartDate.Text;
string enddate = txtbTaskDueDate.Text;
string status = "0." + percentComplete;
string report = txtbTaskStatusReport.Text;
string affiliates = txtbTaskAffiliates.Text;

string InsertSql = "insert into <removed> (userid, projectid, taskName, startDate, endDate, status, statusReport, affiliates) values(" + userid + ", "
+ associated_project + ", '" + name + "', '" + startdate + "', '" + enddate + "', " + status + ", '" + report + "', '" + affiliates + "')";

lbltest.Text += ddlProject.SelectedItem.ToString() + ", " + ddlProject.SelectedValue.ToString()
+ ", " + ddlProject.SelectedIndex.ToString();

lblSql2.Text = InsertSql;
try
{
PLDBMgr.dbconnect.ExecNonQuery(InsertSql);
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}

MyConnection.Close();

ddlProject.SelectedIndex = 0;
txtbTaskName.Text = "";
txtbTaskStartDate.Text = "";
txtbTaskDueDate.Text = "";
txtbTaskSlider.Text = "";
txtbTaskStatusReport.Text = "";
txtbTaskAffiliates.Text = "";




csharp

ddlProject.Items.Clear();
sql = "SELECT projectName, projectID from projects where userid = " + user;
//DataTable projectData = new DataTable();
DataSet projectData = new DataSet();
OleDbDataAdapter data = new OleDbDataAdapter(sql, srcDB);
data.Fill(projectData, "projects");

ddlProject.Items.Add("<-- Select Project -->");

ddlProject.DataSource = projectData.Tables[0];
ddlProject.DataTextField = projectData.Tables[0].Columns["projectName"].ColumnName.ToString();
ddlProject.DataValueField = projectData.Tables[0].Columns["projectid"].ColumnName.ToString();
ddlProject.DataBind();


This post has been edited by Zehuti: 7 Aug, 2008 - 11:56 AM
User is offlineProfile CardPM

Go to the top of the page


Zehuti
post 8 Aug, 2008 - 10:27 AM
Post #2


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 6

Any ideas at all? I can supply some more code if that is helpful.

Thanks.
User is offlineProfile CardPM

Go to the top of the page

Footsie
post 11 Aug, 2008 - 01:44 PM
Post #3


D.I.C Regular

Group Icon
Joined: 20 Sep, 2007
Posts: 279



Thanked 2 times

Dream Kudos: 50
My Contributions


In my mind I would have done something like that in a SelectedIndexChanged event.
I don't see one in your code.?
In this event you would then update the database etc.
I would have made the listBox auto-postback when clicked.

User is offlineProfile CardPM

Go to the top of the page

Zehuti
post 11 Aug, 2008 - 02:03 PM
Post #4


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 6

QUOTE(Footsie @ 11 Aug, 2008 - 01:44 PM) *

In my mind I would have done something like that in a SelectedIndexChanged event.
I don't see one in your code.?
In this event you would then update the database etc.
I would have made the listBox auto-postback when clicked.


Thanks for your reply,

I'm actually playing with that now, I know I'm doing something wrong/poorly as this seems messy confused.gif I came up with the following code, and got this, I just don't know how to make it do what I want it to do smile.gif By the way, making it autopostback causes the ComponentArt Dialog to close (therefore, removing the dropdown list from the screen).

Thanks again!

CS0407: 'string <removed>.ddlProject_SelectedIndexChanged(object, System.EventArgs)' has the wrong return type

CODE
<asp:DropDownList ID="ddlProject" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddlProject_SelectedIndexChanged" />


(part of the create_task button from above with this line changed)
csharp
string associated_project = ddlProject_SelectedIndexChanged(sender, e);


csharp
public string ddlProject_SelectedIndexChanged()
{
string selectedProject;

selectedProject = ddlProject.SelectedValue.ToString();

return selectedProject;
}
User is offlineProfile CardPM

Go to the top of the page

Zehuti
post 11 Aug, 2008 - 02:38 PM
Post #5


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 6

For the record, I fixed my issue by using a ComponentArt combobox. I won't list this issue as resolved, as this is simply a workaround (if your company has paid for the ComponentArt license) I still don't know what's wrong, and would like to know for future reference. With the combobox, I didn't do anything differently (no auto postback or anything) but it just works..
User is offlineProfile CardPM

Go to the top of the page

Footsie
post 12 Aug, 2008 - 01:49 AM
Post #6


D.I.C Regular

Group Icon
Joined: 20 Sep, 2007
Posts: 279



Thanked 2 times

Dream Kudos: 50
My Contributions


I've never worked with a ComponentArt dialog before.

But your event handler for SelectedIndexChanged looks wrong:
It's not a method, so should not have a return type, it should be void.
The easiest way to add this event handler is - in design view (if using Visual Studio) select the control and above the properties screen (on the right) click the small yellow lightning bolt. Find SelectedIndexChanged and double click.

It should look something like this:
csharp

protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
{
//in the event handler simply set the field you want, to the index that was selected.
associated_project = ddlProject.SelectIndex.ToString();
}


On a separate note, you must be careful of Sql injection when directly inserting user input into your database. Rather use parameters in your code.
Wiki - Sql Injection

This post has been edited by Footsie: 12 Aug, 2008 - 01:51 AM
User is offlineProfile CardPM

Go to the top of the page

Zehuti
post 12 Aug, 2008 - 09:43 AM
Post #7


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 6

I see... Thanks a lot!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 10:08PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month