Wednesday, 11 September 2013

Copy Constructor and/or pop method in framework for a stack class C++

Copy Constructor and/or pop method in framework for a stack class C++

So I have a stack that is made up of randomly generated strings and then a
pointer to the next item in my .cpp code. I then go through and pop each
item off the stack and print them out. I get a seg fault at the end though
so I'm guessing I'm trying to pop an item one past the stack where I do
not own the memory.
I believe my copy constructor is incorrect - maybe it doesn't set the last
value to null in my stack but I can't figure out why it's not setting the
value to NULL when I put the line
newPrev->next = NULL;
here's my code (class only)
#include <string>
using namespace std;
class Stack
{
protected:
struct Node
{
string item;
Node* next;
}; // struct Node
public:
// constructor of an empty stack
Stack ()
{
head = NULL;
}
// copy constructor
Stack( const Stack & rhs )
{
if (rhs.head == NULL) {// check whether original is empty
head = NULL;
}else{
head = new Node;
head->item = rhs.head->item;
Node* newPrev = head;
// Now, loop through the rest of the stack
for(Node* cur = rhs.head->next; cur != NULL; cur = cur->next)
{
newPrev->next = new Node;
newPrev = newPrev->next;
newPrev->item = cur->item;
} // end for
newPrev->next = NULL;
} // end else
}
// destructor
~Stack ()
{
delete head;
}
// assignment
const Stack & operator=( const Stack & rhs )
{
return *this;
}
// query whether the stack is empty
bool empty () const
{
return false;
}
// add an item to the top of the stack
// this method is complete and correct
void push (const string & new_item)
{
Node* new_node = new Node;
new_node->item = new_item;
new_node->next = head;
head = new_node;
}
// remove the item on the top of the stack
void pop ()
{
if (head!=NULL){
Node *n = head;
head = head->next;
delete n;
}
}
// return the item on the top of the stack, without modifying the stack
string & top () const
{
return head->item;
}
private:
Node* head;
};

How to Re-Indent visually selected column in Vim

How to Re-Indent visually selected column in Vim

I have some text like,

I would like to move variable2,var3 and var4 so that all four variables
are aligned together. I am able to visually select the column, using
CTRL-V j j w. But after that how can I reindent or delete the extra spaces
in front of variable2, var3 and var4.
Please not that moving variable1 is not an option as there are multiple
lines that are aligned with variable1.
Thanks.

Unable to create the right query

Unable to create the right query

I need a little help. I am trying to fetch records from database on a
certain critera but unable to make the exact logic that will work out.
Like i have a table in which i am storing some ids like
1,14,23,45,17,8,9,11 etc
I receive an array like this
1,2,19,45,56,65
What I thought was to explode the array and match the individual indexes
to the column in database. But here is the problem..
If I use
Like %value% query, it returns non required results for example if we run
SELECT *
FROM `table`
WHERE `table_skills_id` LIKE '%2%'
LIMIT 0 , 30
You can see that 2 was not in the initial array or column that I wrote
above but because 23 was present there, it responded that there is one
result matching where as it should have said 0 rows matching
What I want is that it matches the whole number rather than matching them
seperately. I have tried "Like %2" as well as "Like 2%" but none of them
works properly.
Any quick help would be highly appreciated.
Thanks.

Logging in users over Http in Windows Forms app

Logging in users over Http in Windows Forms app

I have a WebPages based WebMatrix website with Login and Registration
enabled and I need to be able to Login using my Windows Forms application
aswell.
I'd prefer to not use a Web Browser control, and instead, just create a
Login Form control in my app with a Username and Password field, then
send/POST the form to the https login page, but what I don't know what to
do at this point is how to know if the user has successfully logged in?
How can I check on that from within a Windows Forms app?

DataGridView slow loading new data

DataGridView slow loading new data

I have following code now:
private void LoadNewRegistrations()
{
grdNewRegistrations.EditMode = DataGridViewEditMode.EditOnEnter;
grdNewRegistrations.EditingControlShowing +=
GrdNewRegistrations_EditingControlShowing;
const string cSql = @"SELECT
RegistrationId,RegistrationName,RegistrationStatusText
FROM Registration
LEFT JOIN RegistrationStatus
ON
Registration.RegistrationStatusID=RegistrationStatus.RegistrationStatusID
ORDER BY 1";
const string cSql2 = "SELECT VirtualCategoryName FROM
VirtualCategory ORDER BY 1";
SqlParameter[] oSqlParams = {};
var dgceb = new DataGridViewCheckBoxColumn {Name =
"cebColumn",HeaderText = @"Vybrat"};
var dgcob = new DataGridViewComboBoxColumn {Name =
"cobColumn",HeaderText = @"Virtuální kategorie"};
dgceb.HeaderCell.Style.Alignment =
DataGridViewContentAlignment.MiddleCenter;
DataView vCitems = DTO.Instance.GetDataView(cSql2);
foreach (DataRowView rowView in vCitems)
{
dgcob.Items.Add(rowView[0].ToString());
}
dgcob.Items.Add("-");
grdNewRegistrations.Columns.AddRange(
new DataGridViewColumn[]
{
dgceb,
new DataGridViewTextBoxColumn {Name = "idColumn",
HeaderText = @"ID"},
new DataGridViewTextBoxColumn {Name = "nameColumn",
HeaderText = @"Název"},
dgcob,
new DataGridViewTextBoxColumn {Name = "lastRegColumn",
HeaderText = @"Poslední registrace"},
new DataGridViewTextBoxColumn{Name = "statusColumn",
HeaderText = @"Status"}
});
DTO.Instance.ConnectionString = ConnStringWW;
DataView dvReg = DTO.Instance.GetDataView(cSql, oSqlParams);
grdNewRegistrations.Columns[0].AutoSizeMode =
DataGridViewAutoSizeColumnMode.None; //checkbox
grdNewRegistrations.Columns[0].ReadOnly = false;
grdNewRegistrations.Columns[1].AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCells; //id reg
grdNewRegistrations.Columns[1].ReadOnly = true;
grdNewRegistrations.Columns[2].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill; //name reg
grdNewRegistrations.Columns[2].ReadOnly = true;
grdNewRegistrations.Columns[3].AutoSizeMode =
DataGridViewAutoSizeColumnMode.None; //vc combo
grdNewRegistrations.Columns[3].Width = 150;
grdNewRegistrations.Columns[3].ReadOnly = false;
grdNewRegistrations.Columns[4].AutoSizeMode =
DataGridViewAutoSizeColumnMode.None; //date
grdNewRegistrations.Columns[4].Width = 150;
grdNewRegistrations.Columns[4].ReadOnly = true;
grdNewRegistrations.Columns[5].AutoSizeMode =
DataGridViewAutoSizeColumnMode.None; //status reg
grdNewRegistrations.Columns[5].ReadOnly = true;
foreach (DataRowView dataRowView in dvReg)
{
grdNewRegistrations.Rows.Add(false, dataRowView[0],
dataRowView[1], "-", " ", dataRowView[2]);
}
}
Data (about 5000 records) loads when application starts. It takes about
5sec to load. Now, when I need to load other records (same columns, just
another WHERE clause in sql select condition) when app running it takes
MUCH longer time. I´m using same data to just figure out why its so damn
slow.
private void cbChooseTodayReg_CheckedChanged(object sender, EventArgs e)
{
grdNewRegistrations.Rows.Clear();
LoadNewRegistrations();
}
Another option that I´ve heard is to use DataSource. But I can´t figure
out how to make it in this case (0COL - all checkboxes, 1,2,5COL - from db
on first load, 3COL - combobox with data from db, 4COL just to show info
on combobox value change)

How do I to populate one table with the results of a query from a different table?

How do I to populate one table with the results of a query from a
different table?

I have created a query which works and delivers what I want,however it
will not execute within a batch file.How can I populate another table with
the results of the successfully executed query?
Here is my query
select
isnull (start_charge.account,'No account at start') Start_charge_account
,Finish_charge.account start_charge_account
,case when start_charge.account is null then finish_charge.account else
start_charge.account end "account"
,isnull (start_charge.Branch,99999999999) Start_charge_Branch
,Finish_charge.Branch Finish_charge_Branch
,case when start_charge.branch is null then finish_charge.branch else
start_charge.branch end " branch"
,isnull (start_charge. site_number,99999999999) Start_charge_site_number
,Finish_charge. site_number Finish_charge_site_number
,case when start_charge.site_number is null then finish_charge.site_number
else start_charge.site_number end "site_number"
,isnull (start_charge. service_code, 'No account at start')
Start_charge_service_code
,Finish_charge. service_code finish_charge_service_code
,isnull (start_charge. item_code,'No account at start')
Start_charge_item_code
,Finish_charge. item_code finish_charge_item_code
,isnull (start_charge. why_code, 'No account at start') Start_charge_why_code
,Finish_charge. why_code finish_charge_why_code
,isnull (start_charge. why_type, 'No account at start') Start_charge_why_type
,Finish_charge. why_type finish_charge_why_type
,Start_charge.Start_charge
,isnull(Finish_charge.Finish_charge,0) Finish_charge
,isnull(Start_charge.Start_charge,0)-isnull(Finish_charge.Finish_charge,0)
Variance
from
(
select
account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,sum(charge)
"Start_Charge"
from
cannon_commercial
where
1=1
and record_type = 'PS'
and period = ' 02/07/2013'
group by
account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
) start_charge
full outer join
( select
account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,concat(account,site_number) "account - site"
,ISNULL(sum(charge),0) "Finish_Charge"
from
cannon_commercial
where
1=1
and record_type= 'PS'
and period = ' 04/06/2013'
group by
account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
)
Finish_charge
on Start_charge.Account = Finish_charge.Account
and Start_charge.Branch = Finish_charge.Branch
and Start_charge.site_number = Finish_charge.site_number
and Start_charge.item_code = Finish_charge.item_code
and Start_charge.service_code = Finish_charge.service_code
and Start_charge.why_code = Finish_charge.why_code
and Start_charge.why_type = Finish_charge.why_type

Tuesday, 10 September 2013

Sql query for two select statement

Sql query for two select statement

DateTime startDate = DateTime.ParseExact(txtstart.Text, "MM/dd/yyyy", null);
DateTime endDate = DateTime.ParseExact(txtend.Text, "MM/dd/yyyy", null);
string n1 = DropDownList2.SelectedItem.Text;
if (DropDownList1.SelectedItem.Text == "Membership")// here you can
add selectedindex as well
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from
Membership_det where updateDate between @Start and @End and FID
="+n1+"", con);
adapter.SelectCommand.Parameters.Add("@Start",
SqlDbType.Date).Value = startDate;
adapter.SelectCommand.Parameters.Add("@End",
SqlDbType.Date).Value = endDate;
}
…….. …….. Above is a part of a code to display the data in the grid view.I
am displaying * from Membership_det and also need to display faculty name
from other table…how to add the query with the above query..displaying *
from membership _det table and faculty name from other table