I am trying to move a site from php,mysql to ASP.Net and SQL2005, so I would like not to have to change the database model if possible.
There is a multiple-select checkbox list that stores the values in a table field as a comma-delimited list.
I am using a dropdown list that they choose which item to modify and autopostback to bring it up on the fly below this dropdownlist.
I have a templatefield that has a checkbox list with 4 items in it.
Procedures.
Edit: I have this working. I use the DetailsView1_ItemInserting event to figure out which ones are checked and build a string with their values (ie. item1,item2,item3) <- no comma at the end of the last one
This works and the table has that string as it should.
The problem that I am having is that I can't seem to figure out how to bind the checkboxlist with the selected items when I bring it back up after updating the table with more than one value.
I get a Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
I am unsure of a few things (as I am new to this). Do I have to have that checkboxlist control be unbound and build it in the code-behind, or can I have it be pre-built with it's items as I currently have it (it's not built from a database table as it only has 4 values)
Am I using the wrong tool? Should I be using something else?
The other question that I would have is because I am doing the autopostback on the dropdown list, where would this code be ran? I currenltly have it being run at DetailsView1_DataBinding.
This is what I am using for the editPage.aspx that does work. This checks which ones are checked and builds a string Item1,Item2,etc...:
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
string selections;
if (e.NewValues["special_items"] != null)
{
selections = BuildCheckBoxListString((CheckBoxList)DetailsView1.FindControl("CheckBoxList1"));
e.NewValues["special_items"] = selections;
}
}
public string BuildCheckBoxListString(CheckBoxList list)
{
string s = "";
int count = 0;
for (int counter = 0; counter < list.Items.Count; counter++)
{
if (list.Items[counter].Selected)
{
if (count == 0)
{
s += list.Items[counter].Value;
}
else
{
s += "," + list.Items[counter].Value;
}
count++;
}
}
return s;
}
Here is what I have so far in my editPage.aspx which doesn't work. I can't load the checkboxlist with the proper ones selected based on the data from the database:
protected void DetailsView1_DataBinding(object sender, EventArgs e)
{
int id;
id = Convert.ToInt16(MultiTextDropDownList1.SelectedValue);
CheckBoxList cbl = (CheckBoxList)DetailsView1.TemplateControl.FindControl("CheckBoxList1");
lblTemp0.Text = "id " + id;
// create a sql connection variable
SqlConnection myConnection;
// create a sql command to use
SqlCommand myCommand;
// create a data reader object
SqlDataReader myDataReader;
// create the connection to the sql database
myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["transportation_requestConnectionString1"].ConnectionString);
// open the connection to the database
myConnection.Open();
//prepare sql statements
myCommand = new SqlCommand("SELECT special_items FROM transportation_request WHERE id= @id", myConnection);
// create a parameter to use
SqlParameter param = new SqlParameter();
// add a parameter for the username
param.ParameterName = "@id";
// set the parameter to the id from the dropdownlist
param.Value = id;
// add the parameter to the command
myCommand.Parameters.Add(param);
// excecute the reader with the command we have created
myDataReader = myCommand.ExecuteReader();
// create a string array with an arbitrary loaded amount
string[] vals = new string[10];
// define what to split the string by
char[] split = { ',' };
// loop through the reader store the split parts
while (myDataReader.Read())
{
// split the string and put it into the array
//Response.Write(myDataReader[0].ToString());
vals = myDataReader[0].ToString().Split(split);
} // end of while
// Response.Write("count " + count);
// lblTemp0.Text = vals[0].ToString() + "<br />";
// now we need to split the values based on the comma
//make sure no existing selections in the list
cbl.ClearSelection();
// loop through the previously selected items taken from the db
for (int j = 0; j < vals.Length; j++)
{ // loop through the items in the list and check if any equal the db value
foreach (ListItem li in cbl.Items)
{ // if they are equal, change that item to a selected value
if (li.Value == vals[j])
li.Selected = true;
} // end of foreach loop
} // enf of for loop
//cleanup objects
myDataReader.Close();
myConnection.Close();
}
I did check the vals array and it is listing them properly, but it seems to be databinding to the original value still.
This leads me to believe that I should be using a e.Values type property to modify something there, but I haven't seen anything like that online anywhere and it isn't available for the CheckBoxList1_DataBinding event.
See, told you I was new to this!
One more thing, I may be so bold as to ask for EVEN MORE. Can I refer to my sqldatasource from the code-behind? I have tried with no success, so I created the datareader in the code-behind to get the same thing that my SqlDataDetailsView1 has.
I am most open to some constructive criticism on how to better my code as well.
Rich