How to insert multiple asp.net checkboxes values to MSSQl database as comma seperaed string 1,2,3,4,5 using vb.net? - asp.net

How to insert and retrive multiple asp.net checkboxes values to MSSQl database as comma seperaed string 1,2,3,4,5 using vb.net ?
and retrieve the inserted checkbox chekched in disabled form ..using vb.net
example of this is :
http://www.redbus.in/Booking/SeatSelection.aspx?rt=4017230&doj=31-Dec-2010&dep=04:55%20PM&showSpInst=false
I want this type of whole layout in vb.net ... means if user registered selected seats then then next the same page load for todays date the selected seats will be disabled ...
pleas provide me the code snippet for that // insert and retrieve funda... in checkbox

Try with such a loop
string str = "";
for (int i = 0; i <= 29; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
string b = CheckBoxList1.Items[i].Value;
str += b + " , ";
}
}
than make an insert statement and you are putting all the checkbox values into a column.
than just call select statement call the column

You are doing a very bad thing storing lists of values in a single MSSQL column. I know it may be convenient now, but it is always eventually bad news. Why not store booked seats in their own table, one seat per row? When you store a list of values in a column, you restrict access to the information stored in that column to only the code that can split the list. It isn't available to database queries or to other code as a list, merely as a string. You will end up replicating the code to split this string to other places that need the list data.

Related

Search string which contains multiple words using CATSEARCH query in PL/SQL

String record : Blueoba Mountain Company
SQL Query :
SELECT from table
WHERE CATSEARCH(account_partner_name_type,'%Blueoba% %Mountain% %Company%', NULL) > 0)
where rn <=500;
If I write the full name of the string in the query (i.e.%Blueoba% %Mountain% %Company%) then it gives me the record.
But if I write %Blueoba% %Mountain% %Comp% or %Blue% %Company% or %Comp% then its not returning any record.
So ideally, if I write a word %comp% then it should search all the records which contains 'comp' word and show the records but its not showing.
Can anybody suggest something?
You can try using wild card characters
SELECT * from table
WHERE account_partner_name_type like '%Blueoba%'
and rn <=500;

asp.net SqlDataSource select statement with IN (...) clause

I need to pass a server-side parameter to my SqlDataSource SELECT IN-clause as follows (notice the parameter #InClause and it's location in the SQL-select that is defined in the aspx (for the SqlDataSource):
SELECT UID_REPORT, TXT_RPT_TEXT, TXT_RPT_NAME, TXT_RPT_ASPX_PAGE, TXT_RPT_TYPE
FROM REPORTS WHERE (UID_REPORT IN (#InClause))
ORDER BY INT_SORT_ORDER
But this does not pass validation in the Test-Query operation.
The '#InClause' parameter gets it's value from a HiddenField control.
I want to set this value based on some conditions. for example If DIVISION=5 then #InClause would ="45,46" Else ="14,15".
Any suggestions on how to pass the IN ( ... ) values by the hiddenfield.Value (which is a string, of course).
Thanks.
You need split string function to do this
SELECT uid_report,
txt_rpt_text,
txt_rpt_name,
txt_rpt_aspx_page,
txt_rpt_type
FROM reports
WHERE uid_report IN (SELECT split_value
FROM Udf_splitstring(#InClause, ','))
ORDER BY int_sort_order
Check the below links to create a split string function
http://sqlperformance.com/2012/07/t-sql-queries/split-strings
http://www.sqlservercentral.com/articles/Tally+Table/72993/
or of-course you can use dynamic sql

Insert data in second table using data inserted in first

I'm trying to use a form to gather data and store them in a table and immediately do some calculations on that data and store the results in a separate table..
But I don't seem to be able to read the first table immediately after insertion..
I always encounter a zero in my calculation meaning that the first table is considered empty when trying to calculate (but is not empty when the page is refreshed, meaning that the data finally finds its way to the first table anyway) ..
This is the example:
#{
// this is the code that gathers data from a form and stores the data in a table
foreach(var row in db.Query(selectquestions))
{
var questionid = row.QuestionId;
var answer = Request["Q" + row.QuestionId.ToString()];
var insertanswers = "INSERT into Answers (UserId,QuestionId,Answer,ExamId) VALUES(#0, #1, #2, #3)";
db.Execute(insertanswers,userid,questionid,answer,1);
}
// this is the code that tries to immediately use the data stored by the above statement and store the new calculation results in a separate table
int totalquestions= db.Query( some query using the first table ).Count();
int correctanswers= db.Query( some query using the first table ).Count();
int incorrectanswers=db.Query( some query using the first table ).Count();
var score= (float)(correctanswers * 3 - incorrectanswers) / (totalquestions * 3)*100;
var insertscores = "INSERT into Scores (UserId,ExamId,ItemId,Score) VALUES(#0, #1, #2, #3)";
db.Execute(insertscores,userid,1,1,score.ToString());
}
I'm using web pages/razor/SQL Server CE .. any advice would be appreciated

Edit the code of a GridView column asp.net

I've been looking for a way to control the text that appears in a particular column of a GridView.
For example consider a database with two tables Student and Class.
I want to add a column to the GridView which print out all the students in the Database, the column will show the student's class name, how can do it? (I can normally print the ClassId since its a FK in the student table)
I want to add a column to the GridView which print all the classes, the column will count the number of students in each class, how can I do it?
1- You can do that simply with inner join or a stored procedure to get the class name beside all the data you need in your query.
2- More than one way to do what you want:
For example:
you can add a column to your data table (empty column ), and fill it later through using Sum() aggregate function in a query.
DataTable result_dt = DAL_Helper.Return_DataTable(sqlSelect);//your original query
result_dt.Columns.Add("NumberOfStudent");
result_dt.Columns["NumberOfStudent"].DataType = typeof(string);
result_dt.Columns["NumberOfStudent"].MaxLength = 255;
if (result_dt.Rows.Count > 0)
{
for (int i = 0; i < result_dt.Rows.Count; i++)
{
//Here u can fill your new empty column.
}
}
result_dt.AcceptChanges();
After you return your customized data table(as a data source), you can bind it to the grid view.
Another solution : add an empty column to the grid view and in the RowDataBound event of the grid view you can fill this column through some loop and you can use LINQ to help you in getting the summation.

perform "not in" query using linq from 2 datatables

i have a datatable which contains "InvalidCodes".
Before uploading the data to database(data is still in datatable), i want to perform linq on the datatable to remove Invalid entries and move them in another datatable
datatable allEntries ( entries yet to be uploaded in database)
datatable InvalidCodes(single column datatable - retrieved from database)
datatable invalidEntries
right now "allEnties" contains valid entries and invalid entries. the linq query on "allEntries" should move the nonexistend code entries to invalidEntries datatable.
plz help me perform this.
below is the query i formed but its not valid
string query = "select [CityCode] from [StateCity] ";
DataTable citylist = getDataTableFromSelect(query);
var result = from myrow in inputDt.AsEnumerable()
where !myrow.Field<string>("CityCode").Contains(from myrow2 in citylist.AsEnumerable() select myrow2.Field<string>("CityCode") )
select myrow;
I'd make a HashSet for the invalid city codes - this will allow the code to quickly/efficiently identify which of the codes are in the invalid set.
e.g. something like:
var invalidCityCodes = from myrow2 in citylist.AsEnumerable()
select myrow2.Field<string>("CityCode");
var invalidCityCodeHashSet = new HashSet<string>(invalideCityCodes);
var result = from myrow in inputDt.AsEnumerable()
where !invalidCityCodeHashSet.Contains(myrow.Field<string>("CityCode"))
select myrow;
You can also take both the results in 2 Different Lists and then you can
use
List1 = List1.RemoveAll(Item=>List2.Contains(Item))
This works fine with me and will work for you also.

Resources