hey guys
i have a small windows forms app with a simple seies of textboxes of int data type (which i created using the local database) and a button.
when i click the button it will simply fill the textboxes with text data
thing is should i be assigning everything to variables first and then using the variable or should i just stick to directly assigning values to the objects? here is an example of what i mean:
total_OutgoingTextBox.Text = "35";
can i do this? it sure does work but i was thinking, should i be assigning this to a variable first then using the variable instead?
if so i wasnt quite sure how that would be done, here was my attempt to do it (it didnt work though but could find no other way) ... :(
string tb1 = total_OutgoingTextBox.Text.ToString();
tb1 = "35";
why wont this work? i even tried taking out the .Text as well. it will compile ok but wont populate the textbox with the 35 i want.
thanks
Loading
Sam HobbsPosted Jan 3, 2011, 6:19 PM
theLizardPosted Jan 3, 2011, 5:27 PM
I actually wrote that piece about 4 years ago, it is part of much larger collection of functions, methods and custom controls, Sam is right about MS doing similar program but it is your choice weather to use the MS x-spurts code or write your own to learn how it is done by those x-spurts, I prefer to write my own code that way I can deal with the side effects of the code whereas using some one else's code that you can't get to, you can't and will forever be writing work around code to solve problems you should not have to.
To be a good programmer you need to open your mind and to work outside the box if you only work within the box you only ever see what is in the box, not outside it.
If someone says you can't do that in programming, turn around and do it, if you have to use a third party control to make your job easier, think to yourself, can I make that control myself and learn some more by doing so.
When I wrote my controls using C#2005 it was my second major C# project, the first I learn't the basics c# in web design.
You learn much more by doing things yourself than by using someone else's code, you get to learn the fundamentals by using sql to open tables and get the data from those tables without using a table adapter and filling grids dynamically without using data source objects.
Here is another piece of code for you..
//------------------------------------------------------------------------
internal string createInsertWithValues(string groupName)
{
int gIndex = GetControlGroupIndexByName(groupName);
string valuePart = "";
string ctrlName = "";
string value = "";
string sTmp="(";
if(gn[gIndex].GroupSqlInsert == null){
MessageBox.Show("Could not create Insert Statement ..., Aborting", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return("Aborted");
}
if(gn[gIndex].GroupSqlInsert != ""){
//dose this record depend on a parent id?
bool isGroupDependant = gn[gIndex].GroupDependant;
int parentId = -1;
if(isGroupDependant){
parentId = getGroupRecordId(gn[gIndex].DependantGroupName);
}
else{
if(gn[gIndex].ParentRecordId !=-1)
parentId = gn[gIndex].ParentRecordId;
}
//get value part of statement
valuePart = gn[gIndex].GroupSqlInsert.Substring(gn[gIndex].GroupSqlInsert.IndexOf("VALUES(")+7);
if(valuePart.Substring(valuePart.Length-1,1)== ")")
valuePart = valuePart.Substring(0, valuePart.Length-1); //get rid of the ) at end of string
if(valuePart.Contains("#")){
string a = valuePart.Substring(0, valuePart.IndexOf(","));
valuePart = valuePart.Replace(a, "");
valuePart = valuePart.Trim();
if(valuePart.Substring(0, 1) == ","){ valuePart = valuePart.Substring(1); }
a = a.Replace("#", "");
sTmp += a + ", ";
}
valuePart = valuePart.Trim();
while(valuePart.IndexOf("@") >= 0){ // as long as there is an @ in the string
//get the control name from the string and strip the @ indicator
//and the last ' if it exists
int start, end;
start = valuePart.IndexOf("@")+1; //get ctrl name start
end = valuePart.IndexOf(",")-1; //get ctrl name end
if(end <= 0)
end = valuePart.Length-1; //get ctrl name if no , found
ctrlName = valuePart.Substring(start, end); //get value from ctrl
if(valuePart.IndexOf(",") > 0)
valuePart = valuePart.Remove(start-1, valuePart.IndexOf(",")+1);
else
valuePart = valuePart.Remove(start-1, valuePart.Length);
value = getControlData(gIndex, ctrlName);
value = common.checkSingleQuote(value);
sTmp += value + ",";
}
}
if(sTmp.Substring(sTmp.Length-1, 1) == ","){
sTmp = sTmp.Remove(sTmp.Length-1);
sTmp += ")";
}
else{
sTmp += sTmp.Substring(1, sTmp.Length-1) + ")";
}
valuePart = sTmp;
//re-construct insert statement
sTmp = gn[gIndex].GroupSqlInsert.Substring(0, gn[gIndex].GroupSqlInsert.IndexOf("VALUES"));
sTmp += "VALUES" + valuePart;
gn[gIndex].GroupSqlInsert = sTmp;
return(sTmp);
}
//------------------------------------------------------------------------
Sam HobbsPosted Jan 3, 2011, 11:32 AM
John BurnsPosted Jan 3, 2011, 9:23 AM
thanks for the replies and thanks for taking the time to type all that out theLizard.
most of that still is unjnown to me, however; i have copied all of it and when i get more skilled im going to look back at it again and it will be put to good use.
the main thing for now is that i can directly assign the value and all those other things that have been mentioned i have saved and will revise and look at once im upto speed on that sort of level :)
theLizardPosted Jan 2, 2011, 7:25 PM
My first question is how are you getting the data from the db?
My second question is do you want to know the lazy way or the slightly longer programming way where you control all aspects of your program.
The longer way allows you to create a class which can be instantiated a number of times within a single form
Assigning db values to variables is what I do in almost all situations but then I do not declare individual variables to accommodate every text box, combo box etc..
What I do is something like
(a) create a class say recordValues
(b) in this class you create a struct of say, fieldProperties which would contain something like
int datatype;
string fieldName;
string fieldValue;
(c) in the same class declare an array of fieldProperties with 0 elements ie fieldProperties[] fieldProps = new fieldProperties[0];
in your form you can then instantiate your class any number of times to represented any number of tables you want to assign field values.but this requires you to make your own db connections and loop through the result set and assign each value to a new element of fieldProps.
I am putting this code in for example only do not even try to compile it, it will not work no matter what you do to it, this is one function of many others that is needed to do what it does
In simple terms, this is one of many functions of a control that when placed on a form will load records (show in text boxes etc.), edit or delete without writing any code on the form to do so, it does need custom controls derived from the standard controls with additional properties though. I put this here simply to show how complex things can be when you do things the long way, the upside is that you only do these things once and can then use in any other project you want simply by dropping the controls on your forms.
try to read it and work out what it is doing, if you want answers to parts you don't understand let me know.
internal static bool getRecord(lizardControlAgent fg, int gIndex)
{
int i=0;
int ctrlCount = fg.gn[gIndex].GroupCount;
string TableName = fg.gn[gIndex].TableName;
string sql = "SELECT ";
string field = "";
bool allowNull = true;
object value = null;
lizardSQLSource sqlSource = new lizardSQLSource();
if(fg.gn[gIndex].WantGroupRecordId){
sql += fg.gn[gIndex].RecordIdFieldName + ", ";}
for(i = 0; i
//get control column Name and build sql statement
sql += fg.getColumnName(c);
if(i < ctrlCount-1){
sql += ",";
}
}
//add FROM part
sql += " FROM " + TableName;
//do we want a where part
if(fg.gn[gIndex]._sqlWhereForGet != ""){
sql += " " + fg.gn[gIndex]._sqlWhereForGet;
}
fg.SqlDataSource.SqlStatement = sql;
fg.SqlDataSource.Command = fg.SqlDataSource.newCommand(fg.SqlDataSource.ServerSource, sql);
if (fg.SqlDataSource.Command == null || !fg.SqlDataSource.ExecuteReader(fg.SqlDataSource.Command))
return(false);
if(fg.SqlDataSource.read())
{
if(fg.gn[gIndex].WantGroupRecordId){
if(fg.gn[gIndex].RecordIdFieldName != ""){
fg.gn[gIndex].GroupRecordId = int.Parse(fg.SqlDataSource.get(fg.gn[gIndex].RecordIdFieldName));
}
}
for(i = 0; i
field = fg.getColumnName(c).ToString();
//set allow null here
sqlSource.ServerSource = fg.SqlDataSource.ServerSource;
sqlSource.sqlConnect = true;
allowNull = fg.SqlDataSource.AllowNull(fg.gn[gIndex].TableName, field, sqlSource);
fg.setNullable(c, allowNull);
value = (object)fg.SqlDataSource.get(field);
value = setValueByDataType(value, getDataType(sqlSource, field, TableName));
fg.ctrlSetValue(c, value);
}
}
fg.SqlDataSource.terminate();
return(true);
}
Sam HobbsPosted Jan 2, 2011, 6:21 PM
Some people will say you should use a local variable instead of a property of the control, but I do not think that makes a difference.
John BurnsPosted Jan 2, 2011, 7:39 AM
i have now publish my app but now discovered a problem.
EDIT** when i delete one of the records by clicking on the red X i get an error message saying Concurrency Violation: the DeleteCommand affected 0 of the expected 1 records.
why might this apear? i cant seem to get it to go away, i even built my program from scratch again but same thing. i dont know if it has anything to do with the button i added that simply populates the textboxes for me?
Santhosh NPosted Jan 2, 2011, 7:04 AM
tb1 = "35";
here, you are trying to assign to a variable but not textbox so you could not see in textbox when you run your application..
if you assign thos back to the textbox, you can see it in textbox in your application
total_OutgoingTextBox.Text = tbl;
To the original ques, you need not bring in variables in between to assin to the textboxes, and you could directly assin
like
textBox1.Text = "45";