|
Thanks Bill.
I'm studying some code examples.
Thanks for letting me know about the upgrade in C# causes things to happen in the background. Maybe some code examples are for the older version of C#.
In your reply you said
Start here: [^], [^], [^]. And, listen to Jon Skeet: [^]
I'm, not certain if there was a site address that you wanted me to go to?
Brian
|
|
|
|
|
Each of those ^ links should take you to relevant content ... if they're broken, let me know.
May I suggest re-framing your concern from "Selecting the correct code" to getting a good grasp of C# language fundamentals so that, in the future, you can make wise choices about what semantics and structures to use ?
You have the possibility of 'static variables, as well, and, the options for access: 'public, 'private, 'protected, 'internal.
C# is a very rich, and deep, language: for better, and worse, there are multiple ways to implement many fundamental structures, and relationships.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Hi Bill.
I'm trying to get a good understanding of C# and these are some of the things that beginners to C$ have trouble with but I'm hoping that the more I study the code and try writing some small programs of my own then everything should fall into place.
I'm more use to using Basic (in the past) which had global variables.
Brian
|
|
|
|
|
A combination of study, and coding small-scale examples that use the information and concepts you are studying, is an excellent way to learn.
You can search CodeProject to find books that I, and other people, have recommended. This one is free (in English): [^] ... [^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Thanks Bill for the useful links. Lots of useful information at these links.
Brian
|
|
|
|
|
When you use <pre> tags, please make sure you select the correct language from the dropdown list that appears when you click the code button above the edit box. And also make sure that the checkbox marked "Treat my content as plain text, not as HTML" below the edit box, is unchecked.
|
|
|
|
|
I was told I need to add a tag at the start or end of my code so it is formatted correctly when posting code on this discussion group. I've forgotten what the tag is.
Can someone please tell me.
|
|
|
|
|
PRE tags. You can just highlight was you pasted in and hit the "code" button just about the Message box.
|
|
|
|
|
|
I've tried multiple c# programs that I've found on the web, including this one from Code Project:
.NET Wrapper for ChangeDisplaySettingsEX[^]
I'm not sure why but I've never been able to successfully change my secondary display. NOt sure what I'm missing? Can someone provide any insight?
|
|
|
|
|
Try sharing your code so we can see what you are missing?
|
|
|
|
|
Message Removed
modified 14-May-19 11:59am.
|
|
|
|
|
Message Removed
modified 14-May-19 11:59am.
|
|
|
|
|
Message Removed
modified 14-May-19 11:59am.
|
|
|
|
|
I have a list of strings and I want to join all of them as single line. Here spacings are the key, I Have to read the existing line and I need to maintain the same formatting. for example
Existing Lines:
BBSS14 CODE SIZE 253 1.25 1 800 EXTENDED 1.52 89.39
B4 CO S 3 1.1 5 80 EXTE 22 89.3669
BB14 CODEER SIZE 53 1 12 1147 EX 2.852 9
BB14 CODE SE 22253 0.1 1 1 EXTED 2.852 69
BB14 DOERERR SIZE 3 3 525 0 EXTEND 2.82 89.0
I have to read any one of the above lines and set the formatting.
If we see the above example some columns are left aligned and some are right aligned we have to maintain the same format for the given list of strings.
If somebody know the string formatting plz help me.
|
|
|
|
|
We can't give you exact instructions because we have no idea how you are building yout strings, but if you want specifically formatted output, you have to specify that when you build the string.
There are a couple of ways to do that.
For string values, you can use PadLeft:
string abc = "ABC";
string abcdefgh = "ABCDEFGH";
Console.WriteLine(abc.PadLeft(8));
Console.WriteLine(abcdefgh.PadLeft(8));
Which will give you:
ABC
ABCDEFGH For numbers, it's more complicated: Aligning strings within string.Format and interpolated strings[^]
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
somewhat OT, but, I have always found this behavior of PadLeft weird: Quote: A new string that is equivalent to this instance, but right-aligned and padded on the left with as many spaces as needed to create a length of totalWidth. However, if totalWidth is less than the length of this instance, the method returns a reference to the existing instance. If totalWidth is equal to the length of this instance, the method returns a new string that is identical to this instance.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
You do have to wonder what the SQL Server designers were smoking when they came up with that, yes ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
anilkolla wrote: If we see the above example some columns are left aligned and some are right aligned we have to maintain the same format for the given list of strings. That's not random on a column. Text is aligned different from numbers. All you need to do to set the correct alignment is determine whether it is a text or a number.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I assume that the source of the data example shown is some form of structured tabular data. The misalignment in your example is proof of the difficulties involved.
Think about the issues in producing strings whose formats match/mirror the tabular data:
1. font: mono-spaced (simpler) vs. variable width (much more complex)
2. the variation in the length of entries for fields: you will have to do some form of analysis of all the data to figure our for each field the maximum space required.
What is your final goal here ? Print, or screen ? If screen, why not use a UI element designed for tabular data ?
Definitely review the content here to find some possibly useful code tools and resources: [^]. And, note that C# 6 has some new string interpolation features:[^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hesham\Desktop\Database2.accdb");
OleDbDataAdapter da;
DataTable dt = new DataTable();
OleDbCommand cmnd;
public Form1()
{
InitializeComponent();
filldatagridview();
}
void filldatagridview()
{
dt.Clear();
da = new OleDbDataAdapter("Select * From Table1 ", cn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
cmnd = new OleDbCommand("insert into Table1 values('" + textBox4.Text + "','" + textBox3.Text + "'," + Convert.ToInt32(textBox1.Text) + ",'" + textBox2.Text + "')", cn);
cn.Open();
cmnd.ExecuteNonQuery();
cn.Close();
filldatagridview();
MessageBox.Show("added success ", "add", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
textBox4.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox3.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox1.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox2.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
cmnd = new OleDbCommand("Update Table1 Set name ='" + textBox3.Text + "',age = '" + Convert.ToInt32(textBox1.Text) + "', address='" + textBox2.Text + "'Where id='" + textBox4.Text + "", cn);
cn.Open();
cmnd.ExecuteNonQuery();
cn.Close();
filldatagridview();
MessageBox.Show("edit success ", "edit", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button3_Click(object sender, EventArgs e)
{
cmnd = new OleDbCommand("Delete From Table1 Where id='" + textBox4.Text + "", cn);
cn.Open();
cmnd.ExecuteNonQuery();
cn.Close();
filldatagridview();
MessageBox.Show("delete success ", "delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button4_Click(object sender, EventArgs e)
{
Close();
}
}
}
|
|
|
|
|
OK, now you're going to have to explain what you mean by "it doesn't work". That's not a valid problem description.
What DOES happen? What error messages do you get?
In a quick glance, how you're building your SQL INSERT statement is horrible and prone to many problems and even destruction of your entire database. Google for "SQL Injection Attack" for why it's so bad, and then Google for "C# SQL Parameterized queries" for discussions and examples of how to do it correctly.
|
|
|
|
|
Hi,
I'm guessing here, I think your filldatagridview() method only works well once; when you execute it again, it assigns the same DataTable object (possibly with a different content, but that is irrelevant here) as the DataSource to your DGV, hence the DGV doesn't know something should change.
Try adding
dataGridView1.DataSource = null;
before setting
dataGridView1.DataSource = dt;
|
|
|
|
|
Member 14366610 wrote: '" + textBox4.Text + "" Quotes don't match. Learn about parameterized queries.
Also, don't keep connections open outside their method. Also, there's a factory-method on the connection to create commands. They're all disposables, so they should all be in a using-clause.
Start over.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hesham\Desktop\Database2.accdb");
OleDbDataAdapter da;
DataTable dt = new DataTable();
OleDbCommand cmnd;
public Form1()
{
InitializeComponent();
filldatagridview();
}
void filldatagridview()
{
dt.Clear();
da = new OleDbDataAdapter("Select * From Table1 ", cn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
cmnd = new OleDbCommand("insert into Table1 values('" + textBox4.Text + "','" + textBox3.Text + "'," + Convert.ToInt32(textBox1.Text) + ",'" + textBox2.Text + "')", cn);
cn.Open();
cmnd.ExecuteNonQuery();
cn.Close();
filldatagridview();
MessageBox.Show("added success ", "add", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
textBox4.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox3.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox1.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox2.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
cmnd = new OleDbCommand("Update Table1 Set name ='" + textBox3.Text + "',age = '" + Convert.ToInt32(textBox1.Text) + "', address='" + textBox2.Text + "'Where id='" + textBox4.Text + "", cn);
cn.Open();
cmnd.ExecuteNonQuery();
cn.Close();
filldatagridview();
MessageBox.Show("edit success ", "edit", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button3_Click(object sender, EventArgs e)
{
cmnd = new OleDbCommand("Delete From Table1 Where id='" + textBox4.Text + "", cn);
cn.Open();
cmnd.ExecuteNonQuery();
cn.Close();
filldatagridview();
MessageBox.Show("delete success ", "delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button4_Click(object sender, EventArgs e)
{
Close();
}
}
}
|
|
|
|
|