How to get the value of my label? i am using c# - c#

I have this c# form program and i want to get my text label value for some reasons, can somebody help?
I already tried this
String name = myLabel.Text
String name = myLabel.Text.ToString
but it will display none.
conn = dbHelper.getConnect();
conn.Open();
cmd = new SqlCommand("select custID from customer where name = '" + custNameLbl /*this is where i want to get the value*/ + "'", conn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
custIDStorage = Convert.ToInt32(dr[0].ToString()); //this is an int
namename.Text = custIDStorage.ToString(); //this is a label
}
cmd.Dispose();
dr.Close();
conn.Close();

Related

How to get an integer value from SQL database to a variable in windows forms (C#)?

I've created a form called BookSeat which takes no.of seats as an input and calculate the total fare.
Here's my code:
{
try
{
DB db = new DB();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [fare] FROM [dbo].[Train] WHERE name = '" + textBoxName.Text + "' ";
cmd.Connection = db.GetConnection();
db.openConnection();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
int d = dr.GetInt32(0);
int noOfSeats = comboBoxNoOfSeats.SelectedIndex;
int totalfare;
totalfare = noOfSeats * d;
textBoxTotalFare.Text = totalfare.ToString();
db.closeConnection();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This shows and error : Invalid attempt to read when no data is present
SqlDataReader has the method called Read which reads all the data inside sqldatareader. But you have to call this function inside while loop.
try
{
DB db = new DB();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [fare] FROM [dbo].[Train] WHERE name = '" + textBoxName.Text + "' ";
cmd.Connection = db.GetConnection();
db.openConnection();
SqlDataReader dr = cmd.ExecuteReader();
While(dr.Read())
{
//Get value by using column name;
int d = Convert.ToInt32(dr["columnname"]);
int noOfSeats = comboBoxNoOfSeats.SelectedIndex;
int totalfare;
totalfare = noOfSeats * d;
textBoxTotalFare.Text = totalfare.ToString();
}
dr.Close();
db.closeConnection();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

Data in not displayed on checkbox in C# & SQL Server

I am trying to display the values from database. The textbox values are displayed successfully but checkbox values are not displayed, and it shows the error. I get the error shown in this screenshot:
Error Message
My code:
sql = "select * from repair where repairid = '" + repairid + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dread;
con.Open();
dread = cmd.ExecuteReader();
while (dread.Read())
{
checkBox7.CheckState = dread[6].ToString();
}
if(dread[6].ToString = = "True")
{
CheckBox7.Checked = "true";
}
else
{
CheckBox7.Checked = "false";
}

cannot convert from string to int

i want to convert what i selected from the combobox so i can edit it or delete it, but the messege "cannot convert from string to int" keep showing
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
string Query = "select * from tbl_article where NameArticle='"+comboBoxArt.Text+"'";
SqlCommand cmd = new SqlCommand(Query, sqlCon);
SqlDataReader myReader;
try
{
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
txtName.Text = myReader.GetString("NameArticle");
txtPrice.Text = myReader.GetInt32("PriceArticle").ToString();
}
}
and also when i run it, the selected item changes to his id "IdArticle".
how can i fix this ??
Why not just do this?
txtName.Text = myReader["NameArticle"].ToString();
txtPrice.Text = myReader["PriceArticle"].ToString();
It should get whatever value from database whether its int or DateTime etc then convert it to string?

TextBox.Text Updated On DropDownList_TextChanged

I have a DropDownList connected with an sqlDataSource and a TextBox .
I want every time the user selects a name from the column name listed on the DropDownList,
the value of the column id of that item being displayed on the text of the TextBox
I made this code but doesnt seems to work:
(the code contains no errors)
protected void DropDownListIliaka_SelectedIndexChanged(object sender, EventArgs e)
{
string conString = "Data Source=icsd-db.aegean.gr\\icsdmssqlsrv;Initial Catalog=icsd12015;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.Text + "'";
SqlCommand cmd = new SqlCommand(cmdText, con);
try
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
iliako_sistima_id.Text = (reader["Iliako_Sistima_ID"].ToString());
}
}
}
finally
{
con.Close();
}
}
this
string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.Text + "'";
should be
string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.SelectedItem.Text + "'";
Hope this helps
You must set the property AutoPostBack of DropDownList to true, so it posts back to the server and sets the value of textbox

on selecting the dropdownlist 2 nd time textbox value not showing blank

I have written following code.
my 1st orderid has both records for process 1 and process 2
but my 2nd orderid has record only for process 1.
when i select orderid 2 after selecting orderid 1 it keeps the value as it is for orderid 1 in process 2 textbox.
i want that textbox to be blank
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DbConnect objdbc = new DbConnect();
SqlConnection con = objdbc.openConnection();
SqlCommand cmd = new SqlCommand("SELECT [orderid],[processid],[orgid],[processdesc] ,[perwet] FROM [anghan].[dbo].[ProcessOrder] where orderid='" + DropDownList1.SelectedItem.Value + "' and processid=1 ", con);
cmd.CommandType = CommandType.Text;
// cmd.Parameters.AddWithValue("#Id", value);
cmd.Connection = con;
//con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox2.Text = dr["perwet"].ToString();
// DropDownList1.Items(DropDownList1.SelectedIndex).Text = dr["service"].ToString();
}
dr.Close();
//process 2
SqlCommand cmd2 = new SqlCommand("SELECT [orderid],[processid],[orgid],[processdesc] ,[perwet] FROM [anghan].[dbo].[ProcessOrder] where orderid='" + DropDownList1.SelectedItem.Value + "' and processid=2 ", con);
cmd2.CommandType = CommandType.Text;
// cmd.Parameters.AddWithValue("#Id", value);
cmd2.Connection = con;
//con.Open();
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
TextBox3.Text = dr2["perwet"].ToString();
// DropDownList1.Items(DropDownList1.SelectedIndex).Text = dr["service"].ToString();
}
dr2.Close();
Why do not you set the TextBox2 and TextBox3 to empty in the starting of the code . So that you do see what you have populated . Only those textbox value will be change where your code will reassign otherwise it will keep the old value.
just put like this>>
if(dr!=null)
{
while (dr.Read())
{
if((dr["perwet"]!=null)||(dr["perwet"].ToString()!=""))
TextBox2.Text = dr["perwet"].ToString();
else
TextBox2.Text="";
// DropDownList1.Items(DropDownList1.SelectedIndex).Text = dr["service"].ToString();
}
}
It will work.

Categories