asp.net setting a list item to selected value from mysql database

mgh

New Member
I have the following code on my aspx page what i wish to do is set the selected default value from a MySql database upon page load whilst still retaining the 50 list items for when the user wishes to update the stored value inside the mysql database. If there is no value stored inside the database then the default value to be set as 0 as it is shown at the time being. I've tried a few methods so far but the ones i tried wipe the current list items that i have.\[code\]<asp:DropDownList ID="whmlst" runat="server" CssClass="dropDownJobLevel"> <asp:ListItem Selected="True">0</asp:ListItem> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> <asp:ListItem>6</asp:ListItem> <asp:ListItem>7</asp:ListItem> <asp:ListItem>8</asp:ListItem> <asp:ListItem>9</asp:ListItem> <asp:ListItem>10</asp:ListItem> <asp:ListItem>11</asp:ListItem> <asp:ListItem>12</asp:ListItem> <asp:ListItem>13</asp:ListItem> <asp:ListItem>14</asp:ListItem> <asp:ListItem>15</asp:ListItem> <asp:ListItem>16</asp:ListItem> <asp:ListItem>17</asp:ListItem> <asp:ListItem>18</asp:ListItem> <asp:ListItem>19</asp:ListItem> <asp:ListItem>20</asp:ListItem> <asp:ListItem>21</asp:ListItem> <asp:ListItem>22</asp:ListItem> <asp:ListItem>23</asp:ListItem> <asp:ListItem>24</asp:ListItem> <asp:ListItem>25</asp:ListItem> <asp:ListItem>26</asp:ListItem> <asp:ListItem>27</asp:ListItem> <asp:ListItem>28</asp:ListItem> <asp:ListItem>29</asp:ListItem> <asp:ListItem>30</asp:ListItem> <asp:ListItem>31</asp:ListItem> <asp:ListItem>32</asp:ListItem> <asp:ListItem>33</asp:ListItem> <asp:ListItem>34</asp:ListItem> <asp:ListItem>35</asp:ListItem> <asp:ListItem>36</asp:ListItem> <asp:ListItem>37</asp:ListItem> <asp:ListItem>38</asp:ListItem> <asp:ListItem>39</asp:ListItem> <asp:ListItem>40</asp:ListItem> <asp:ListItem>41</asp:ListItem> <asp:ListItem>42</asp:ListItem> <asp:ListItem>43</asp:ListItem> <asp:ListItem>44</asp:ListItem> <asp:ListItem>45</asp:ListItem> <asp:ListItem>46</asp:ListItem> <asp:ListItem>47</asp:ListItem> <asp:ListItem>48</asp:ListItem> <asp:ListItem>49</asp:ListItem> <asp:ListItem>50</asp:ListItem> </asp:DropDownList>\[/code\]On the code behind page i have this to update the database on list item change which i know works when i check database and also load the values on a different page I am also doing this in C#:\[code\]string st = "Server = localhost; Database = endeavor; Uid = root; Pwd = test"; MySqlConnection connection = new MySqlConnection(st); connection.Open(); string[] sqlupdateJob = new string[18]; sqlupdateJob[0] = "UPDATE userjob SET job_level = '" + whmlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 1"; sqlupdateJob[1] = "UPDATE userjob SET job_level = '" + blmlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 2"; sqlupdateJob[2] = "UPDATE userjob SET job_level = '" + mnklst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 3"; sqlupdateJob[3] = "UPDATE userjob SET job_level = '" + brdlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 4"; sqlupdateJob[4] = "UPDATE userjob SET job_level = '" + pldlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 5"; sqlupdateJob[5] = "UPDATE userjob SET job_level = '" + warlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 6"; sqlupdateJob[6] = "UPDATE userjob SET job_level = '" + drglst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 7"; sqlupdateJob[7] = "UPDATE userjob SET job_level = '" + cullst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 8"; sqlupdateJob[8] = "UPDATE userjob SET job_level = '" + gsmlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 9"; sqlupdateJob[9] = "UPDATE userjob SET job_level = '" + armlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 10"; sqlupdateJob[10] = "UPDATE userjob SET job_level = '" + bsmlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 11"; sqlupdateJob[11] = "UPDATE userjob SET job_level = '" + wvrlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 12"; sqlupdateJob[12] = "UPDATE userjob SET job_level = '" + lthlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 13"; sqlupdateJob[13] = "UPDATE userjob SET job_level = '" + alclst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 14"; sqlupdateJob[14] = "UPDATE userjob SET job_level = '" + crplst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 15"; sqlupdateJob[15] = "UPDATE userjob SET job_level = '" + mnrlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 16"; sqlupdateJob[16] = "UPDATE userjob SET job_level = '" + btnlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 17"; sqlupdateJob[17] = "UPDATE userjob SET job_level = '" + fshlst.Text.ToString() + "' WHERE user_id = (SELECT user_id FROM user WHERE username = ?users) AND job_id = 18"; for (int i = 0; i < 18; i++) { MySqlCommand command = new MySqlCommand(sqlupdateJob, connection); command.Parameters.Add("?users", MySqlDbType.VarChar).Value = http://stackoverflow.com/questions/15733539/Label1.Text; command.ExecuteNonQuery(); } connection.Close();\[/code\]
 
Top