Where do you have:
SqlConnection sqlConn = new SqlConnection("connString");
amd for to Read data, you have to opean a connection as well:
sqlConn.Open();
And its good to close it on the end of the code (or use "using" keywords instead).
Where do you have:
SqlConnection sqlConn = new SqlConnection("connString");
amd for to Read data, you have to opean a connection as well:
sqlConn.Open();
And its good to close it on the end of the code (or use "using" keywords instead).
YOu have to set some more parameters, like position (Location), size (if necessary), name, ...:
//inside click event handler
string submitWord = tempWord;
Label lbl = new Label();
lbl.Name ="label1";
lbl.Text = submitword;
lbl.Location = (20, 20); //set your location
lbl.AutoSize = true;
flowLayoutPanel.Controls.Add(lbl);
Yes, "partial" keyword is missing.
You are welcome.
If this is was helpful upVote, or if this was it, mark the thread as answered, so we close it.
No problem. You can mark the thread as answered now.
thx ina advance.
i dont have any experience with datagridview so kindly express your solution in bit detail
hmmm, this will take some time then... ;)
Ok, regarding to your thread`s title, you want to pass data from textBox to DGV. You can do it this (Simple) way:
dataGrivView1[0,0].Value = txtbox1.Text; //0,0 is 1st column, 1st row
dataGrivView1[1,0].Value = txtbox1.Text; //1,0 is 2nd column, 1st row
dataGrivView1[2,0].Value = txtbox1.Text; //2,0 is 3rd column, 1st row
//for example if you want to pass data to 5th column and 10 row you do:
dataGrivView1[9,4].Value = txtbox1.Text;
One of the possibilities is to use TextChanged event handler:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string lastChar = textBox1.Text.Substring(textBox1.Lenght -1, 1);
if(lastChar == " ")
{
MessageBox.Show("User has just pressed spaceBar.");
}
}
Or use can use KeyPress event, where you will get the last character of whitespace:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsWhiteSpace(e.KeyChar))
{
MessageBox.Show("User has just pressed spaceBar.");
//e.Handled = true;
}
}
Change this method to:
private void AddListBoxItem(object item)
{
if (this.ConvBox.InvokeRequired)
this.ConvBox.Invoke(new AddListBoxItemDelegate(AddListBoxitem), new object[] {item});
else
this.ConvBox.Items.Add(item);
}
Try it this way:
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim lstitem As String = ListBox1.SelectedItem.ToString()
Dim sqlQuery As String = "UPDATE laitemtype SET IsActive = @active WHERE itemtypedescription=@lst"
Dim cmd6 As New SqlCommand(sqlQuery, Con)
'I hope oyu have created a new instance of SqlConnection AND Opened it!
cmd6.Parameters.Add("@active", SqlDbType.Int).Value = 1
cmd6.Parameters.Add("@lst", SqlDbType.VarChar, 144).Value = lstitem
cmd6.ExecuteNonQuery()
Next
You put create labels one beside another (lets say a few inches apart), and because you didnt disable AutoSize property, it means that the 1st label has Width size of 35 (35 is defualt), so your code only shows 1st letter A, other were hidden. Am I correct?
That is becuase your 2nd label (B letter) was hidden behind label of letter A.
My code has set the AutoSize to false, and set new width Size to 15. 15 is the width of a letter (and a bit more, so you dont have letters just one beside another).
This will do it:
public partial class Form1 : Form
{
Label[] lbls;
public Form1()
{
InitializeComponent();
char[] letters = Enumerable.Range('A', 'Z' - 'A' + 1).Select(i => (Char)i).ToArray();
lbls = new Label[letters.Length];
int x = 20;
int y = 20;
for (int i = 0; i < letters.Length; i++)
{
lbls[i] = new Label();
lbls[i].Name = "label" + letters[i];
lbls[i].Text = letters[i].ToString();
lbls[i].Location = new Point(x, y);
lbls[i].AutoSize = false;
lbls[i].Size = new Size(15, 13);
lbls[i].Cursor = Cursors.Hand;
lbls[i].Click += new EventHandler(labels_Click);
this.tabPage1.Controls.Add(lbls[i]);
x += 16;
}
}
private void labels_Click(object sender, EventArgs e)
{
Label lb = sender as Label;
MessageBox.Show("You have clicked " + lb.Text);
}
}
use SelectedItem property of comboBox:
comboBox1.Selecteditem.ToString();
I am glad I could help you.
Maybe you have to change the Timer namespace to:
System.Windows.Forms.Timer timer1;
//when creating a new instance you do:
timer1 = new ystem.Windows.Forms.Timer();
What you have to do is:
- put three buttons on the forms (do not chaange their namee, by defualt they will be names: button1, button2, button3)
- put a textBox
- and put a label over textBox
(so you only have to put 5 controls explained above).
Then you simple copy / paste the code I gave you. But be careful on th Form1 name. Form1 is by defaurt, if you change the project name when creating it, it will have different name.
public partial class Form1 : Form
{
Timer timer1;
Button[] btns;
public Form1()
{
InitializeComponent();
timer1 = new Timer();
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
//create an array of buttons:
btns = new Button[] { button1, button2, button3 };
}
protected override void OnShown(EventArgs e)
{
//set the button1 to be focused on the beginning:
button1.Focus();
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < btns.Length; i++)
{
if (i.Equals(2))
timer1.Stop();
else if (btns[i].Focused)
{
btns[i + 1].Focus();
break;
}
}
}
}
Use better see (or use) a doctor, if you are in troubles like you are now.
What error?
YOu have to tell me it.
Try to change it to:
con.ConnectionString = strconnection
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim str As String = "Select CUSTOMERID from tbl_Customer"
cmd = New SqlCommand(str, objconnection)
Dim dr As SqlDataReader = cmd.Executereader()
While (dr.Read())
'If the id column is not integer, chnage "int" with correct type (stirng I would say)!
TXTCUSTOMERD.Text = CInt(dr("CUSTOMERID"))
End While
If String.IsNullOrEmpty(TXTCUSTOMERD.Text) Then
TXTCUSTOMERD.Text = 0
TXTCUSTOMERD.Text = Conversion.Val(TXTCUSTOMERD.Text) + 1
Else
TXTCUSTOMERD.Text = Conversion.Val(TXTCUSTOMERD.Text) + 1
End If
dr.Close()
Is maybe this what you have been looking for:
int[,] array = new int[2, 4] { { 1, 2, 3, 4 }, { 1, 2, 3, 4 } };
List<int> list = new List<int>();
for (int i = 0; i < array.GetLength(1); i++)
{
int sum = 0;
for (int j = 0; j < array.GetLength(0); j++)
{
sum += array[j, i];
}
list.Add(sum);
}
//list now has all sums of 2-d array!
//result is: 2,4,6 and 8
If not, let me know.
Oh, I see, I sure did miss it. Lately I wasnt around much. I can see there is a whole other thread for this issue. So I wont even interrupt.
YOu didnt start coding the correct way. You have to put buttons into an array, and loop through them and create a common event for them, like this:
//on form load on in constructor:
Button[] btns = new Buttons[]{button1, button2, button2, button4, button5, button6, button7, button8 };
foreach(Button btn in btns)
btn.Click += new EventHanlder(buttons_Click);
private void buttons_Click(object sender, EventArgs e)
{
Button button = sender as Button;
//clicked button is not in "button" variable.
//exmaple:
string buttonName = button.Name;
string butonText = button.Text;
//you can now opperate wtih them..
}
Delegates are used to add methods to events dynamically.
Threads run inside of processes, and allow you to run 2 or more tasks at once that share resources.
Another explanation:
Delegates: Basically, a delegate is a method to reference a method. It's like a pointer to a method which you can set it to different methods that match its signature and use it to pass the reference to that method around.
Thread is a sequentual stream of instructions that execute one after another to complete a computation. You can have different threads running simultaneously to accomplish a specific task. A thread runs on a single logical processor.
Hi, try it this way:
public class testing
{
delegate void mydelegate(string msg);
public void btnStart_Click(object sender, EventArgs e) // Click Event
{
ThreadStart ts = new ThreadStart(StartServer);
Thread t = new Thread(ts);
t.Start();
}
public void StartServer()
{
str = "Hello";
function1(str);
}
public void function1(string str)
{
if(listBox1.InvokeRequired)
listBo1x.Inkvoke(new mydelegate(function1), new object[] { str });
else
listBox1.Items.Add(str);
}
}
Oh OK. I'm not relieved after all :=( EditOnEnter lets me edit any of my columns and I don't want that. This is getting frustrating...
Sorry, I gave you the wrong code, it was for ComboBox column, sorry ones again.
What is your problem now? Can you please explain it a bit better?
try changing the connect string of ConfigurationManager to:
string connect = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
All the rest stays the same. If the connection string is correct, this has to work.
CellContentClicked is not suitable event to get comboBox clciked (or selected). You you will never get this code into work. Take a look my example, you have to use CheckBox_SelectedIndexChanged event, but since DGV does NOT have this kind of event, you have to do a work around, to use a "normal" comboBox event.
It works like charm :)
Here, I did the code you would like to have.
There is maybe some work to be done in the cell vadidating (data validation; if email and tel.number are correct), and this is about it.
When you do some changes, you press one button, and all it saves automatically (or you update or delete).
Code:
public partial class Form1 : Form
{
private string filePath;
List<Addressar> list;
public Form1()
{
InitializeComponent();
filePath = @"C:\1\Addressar.txt";
list = new List<Addressar>();
}
protected override void OnShown(EventArgs e)
{
GetData();
dataGridView1.AllowUserToAddRows = true;
dataGridView1.DataSource = new BindingSource(list, null);
}
private void GetData()
{
if (System.IO.File.Exists(filePath))
{
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] columns = line.Split(new string[] { "-:-" }, StringSplitOptions.RemoveEmptyEntries);
Addressar adr = new Addressar();
adr.Id = int.Parse(columns[0]);
adr.First = columns[1];
adr.Last = columns[2];
adr.Email = columns[3];
adr.Phone = columns[4];
list.Add(adr);
}
}
if (list.Count == 0)
MessageBox.Show("File exists, but there is no data inside of it.");
}
else
MessageBox.Show("File to read data from it, does not yet exist.");
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (Addressar adr in list)
{
string[] columns = new string[5];
columns[0] = adr.Id.ToString();
columns[1] = adr.First;
columns[2] = adr.Last;
columns[3] = adr.Email;
columns[4] = adr.Phone;
sb.AppendLine(String.Join("-:-", columns));
}
if (sb.Length > 0)
{
System.IO.File.Delete(filePath);
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
using (TextWriter tw = new StreamWriter(fs))
{
tw.Write(sb.ToString());
MessageBox.Show("Update has been done successfully.","Data updating"); …
Here:
private void Form5_Load(object sender, EventArgs e)
{
DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
cmbcolumn.Name = "cmbColumn";
cmbcolumn.HeaderText = "combobox column";
cmbcolumn.Items.AddRange(new string[] { "item1", "item2", "item3" }); //here you can even use dataBinding!
dataGridView1.Columns.Insert(1, cmbcolumn); //or add: dgv.Columns.Add(cmbcolumn);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
private void dataGridViewEkipe_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
string item = cb.Text;
if (item != null)
MessageBox.Show(item);
}
This is it!
You cannot access to AppendText or any property of TextBox over threads. You will have to use delegates to access to it on controls, not on your do, while loop:
namespace sync
{
public partial class Form1 : Form
{
int s = 0;
delegate void MyDelegate1(string msg);
delegate void MyDelegate2(string msg);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
s = 0;
Thread begin1 = new Thread(start1);
Thread begin2 = new Thread(start2);
begin1.IsBackground = true;
begin2.IsBackground = true;
begin1.Start();
begin2.Start();
}
private void start1()
{
do
{
UpdateTextBox1("1");
Thread.Sleep(1000);
Application.DoEvents();
}
while (s == 0);
}
private void start2()
{
do
{
UpdateTextBox2("2");
Thread.Sleep(1000);
Application.DoEvents();
}
while (s == 0);
}
private void UpdateTextBox1(string text)
{
if(textBox1.InvokeRequired)
textBox1.invoke(new MyDelegat1(UpdateTextBox1), new object [] {text});
else
textBox1.Text = text;
}
private void UpdateTextBox2(string text)
{
if(textBox2.InvokeRequired)
textBox2.invoke(new MyDelegat2(UpdateTextBox2), new object [] {text});
else
textBox2.Text = text;
}
private void button2_Click(object sender, System.EventArgs e)
{
s = 1;
}
}
}
This should do the trick!
Then do the query liek this:
string _value = textBox1.Text;
//rest of code, and:
string s = "SELECT id FROM abc WHERE id LIKE 'find%' AND class = @class";
cmd.Parameters.Add("@find", System.Data.SqlDbType.VarChar, 20).Value = _value;
cmd.Parameters.Add("@class", System.Data.SqlDbType.int).Value = 1;
And how do you want to choose different conditions? I really dont understand your question. Please elaborate this issue of yours a bit better.
Do you write any text into textBox, and thats why you use LIKE condition?
If so, you have to change the code to:
string _value = textBox1.Text;
//rest of code, and:
string s = "SELECT id FROM abc WHERE id LIKE 'find%'";
cmd.Parameters.Add("@find", System.Data.SqlDbType.VarChar, 20).Value = _value;
//rest of code...
Your code does not return the name string (value) from all over the method. It reutrns only from the while loop. What in case if the code does not go to while loop? It will return what? notihng. But your method return type is a string. That means you have to return string even if the code does not go to while loop.
Do it like this:
public static string loggedinuser(TextBox uname,TextBox password)
{
string name = null;
readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')");
if (dr.Read())
{
while (dr.Read())
{
name =(dr[0].ToString());
}
}
con.Close();
return name;
}
In case if the code will no go into while loop, it will return a string with null value.
So on the other side you can check it:
string _name = loggeddinuser(uname, password);
if(_uname != null)
{
//your code if the string returns a not null string!
}
else
MessageBox.Show("Value is null."); //or do some other user notification.
I didnt get your question either. Would you please explain your issue a bit n details?
thx in advance.
I didnt get your question either. Would you please explain your issue a bit n details?
thx in advance.
This is another solution:
string yourWord = text.SubString(text.LastIndexOf(" "), text.Lenght - text.LastIndexOf(" "));
I dont know exactly what you are doing, but one dataBase should do with thousand of transactions, but the question is, what is the period? 1000 in one second? that shouldnt be a probelm these day. Read here for some info.
But I doubt you are creating a professional application for some bank or a huge company, that has that many transactions.
So I believe you can handle with one dataBase only.
What is your answer on that?
1st of all, I know its not my business, but why do you create dataBases (or/and their dataTables) in the run time? Why its one not good enought?
There is no point of having more dataBase with the same sintax. You could have only one, and populate it (and getting data out too - like you want to do now).
Its sounds so strange to me, to have planty of equal dataBases.
You might reconsider of re-edit your dataBase - to create only one. And work with only one for ever.
What do you think of that?
Are your column names in all dataBases the same? If they are, you can use dataBinding. Otherwise you will have to populate DGV manually. How? 1st you need to have the same number of columns or less (NOT more)! Then simply loop through each dataTable (each will be filled from the dataBase, using sqlDataAdapter), and through each row of dataTable. This is it!
sp.Value = !String.IsNullOrEmpty(t.HasValue) ? (object)t.Value : DBNull.Value;
Hmmm... this is not an easy answer. It would be better to get your self a book about basics of programming, because question of yours is exactly this. It will take some time to understand what are you asking, and some more to make it work (in complete).
We cannot provide you a simple answer, especially not in one sentance. As you have figured it out by your self, there is plenty to learn (you stated: more you read, more you got confused). Sure, this is not an easy task to learn - as said, this are the fundations, or the basics or programming (in any language). Its called Polymorhism - or inheritance (roughtly said).
You can start with this the simplest examples here.
Take a look at this simple example of Animals:
// Assembly: Common Classes
namespace CommonClasses
{
public interface IAnimal
{
string Name { get; }
string Talk();
}
}
// Assembly: Animals
using System;
using CommonClasses;
namespace Animals
{
public abstract class AnimalBase
{
public string Name { get; private set; }
protected AnimalBase(string name) {
Name = name;
}
}
public class Cat : AnimalBase, IAnimal
{
public Cat(string name) : base(name) {
}
public string Talk() {
return "Meowww!";
}
}
public class Dog : AnimalBase, IAnimal
{
public Dog(string name) : base(name) {
}
public string Talk() {
return "Arf! Arf!";
}
}
}
// Assembly: Program
// References and Uses Assemblies: Common Classes, Animals
using System; …
Sure. You use = mark. This will stsrt writing text from beginning.
Use += :
textBox1 += "Some vlaue" + Environment.NewLine + " Some other valaue";
Take a look at this example, how you access to some other namespace:
namespace Maj16Test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NewNameSpace.NewClass class1 = new NewNameSpace.NewClass();
class1.NewMethod();
}
}
}
namespace NewNameSpace
{
public class NewClass
{
public NewClass()
{
MessageBox.Show("Message in the new namespace`s constructor is showing.");
}
public void NewMethod()
{
MessageBox.Show("Message in the new namespace`s method is showing.");
}
}
}
Explanation to your problem:
You should add reference from Project1 to Project2 to be able to write using namespace MainProject.Project2; To add reference, from Project1 solution explorer references->add reference->browse to locate the project2 assembly or projects tab to get all assembles in all solution including project2 assembly.
textBox is by default set to OneRow Text only. That means that the control does NOT have multiple rows.
If you want to show values in the textBox in rows, you hav to turn on "MultiLine" property (you have to set it to true), and its even good to define the scrollBars to Vertical:
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;
Now you can insert values inthe rows with using Environmetn.NewLine property.
Example:
string a = "a";
string b = "b";
textBox1.Text = a + Environment.NewLine + b;
If you got it, just mark this thread as answered. Thx in advance.
Try this:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(strEmailStranke);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(strObvEmail);
message.Body = "body text"
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("userName, "password"); //of gmail access
smtp.Send(message);
it works for me.
That means that this file is opened. An because it is opened, it means that it is used by another process.
You will have to close it down, and then you can do (execute) your code.
Check doube times if file is in use or not (but I am sure it is in use atm).
Yu have to put creation of the new instance of a Random class on the class level then it will do. So you have to do it, like:
Form1
{
Random generator = new Random();
public Form1()
{
//form1`s constructor
InitializeComponent();
}
public double RandomNumber()
{
//declares random number generator
//returns a random number between 10 and 30
return generator.Next(10, 31);
}
}
Remember, if you will instantite new instance every time you will not get always different number.
You have 3 loops here. It seems strange to me. 1st you loop through rows, and in eqach row you loop through columns, and in each column you loop again through rows od dataTable.
This has no point I would say.
Tell me one thing, what dataSet (dataTable at index 0) holds? Whats in it?
You could loop only through the dataTable, and not trought those two loops x and y.
Please write some of the logic you have there, so I can understand the code a bit better.
Keep what?
I actually didnt understand you well. Did you select any cells (or rows) before calling OnShown method?