yumo
yumo
Explore posts from servers
CC#
Created by yumo on 6/7/2023 in #help
❔ Exporting data in c# winforms button goes wrong
Im having a problem exporting some data from my dgv, heres my code: tha's c# code to export all data visible in the datagridview, my date is like 10-05-2025 but sometimes its exporting with "/" instead of "-" and its changing the date with the month, im from Portugal and i cant figure this out i tried a lot of different things and nothing happend, sometimes some dates come with that , and the others correctly( i also search in the DB if i was inserting the data wrong but im not). Would love some help thanks https://pastebin.com/Wy0W9kD9
6 replies
CC#
Created by yumo on 3/20/2023 in #help
❔ Missing Reference
This is my code:
private void button1_Click(object sender, EventArgs e)
{
_Application excelApp;
_Workbook excelWorkbook;
_Worksheet excelWorksheet;

try
{
excelApp = new Microsoft.Office.Interop.Excel.Application();
excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
excelWorksheet = null;

excelWorksheet = excelWorkbook.Sheets["Folha1"];
excelWorksheet = excelWorkbook.ActiveSheet;
excelWorksheet.Name = "Dados exportados";

for (int j = 0; j < dataGridViewAcoes.Rows.Count; j++)
{
for (int k = 0; k < dataGridViewAcoes.Columns.Count; k++)
{
if (dataGridViewAcoes.Columns[k] is DataGridViewImageColumn)
{
// Column contains an image
byte[] imageData = (byte[])dataGridViewAcoes.Rows[j].Cells[k].Value;
if (imageData != null)
{
// Convert byte array to image
MemoryStream ms = new MemoryStream(imageData);
Image image = Image.FromStream(ms);

// Insert image into cell
Excel.Range range = (Excel.Range)excelWorksheet.Cells[j + 2, k + 1];
range.RowHeight = image.Height;
if (range.RowHeight < 0 || range.RowHeight > 409)
{
// If row height is outside acceptable range, set to default value
range.RowHeight = excelWorksheet.DefaultRowHeight;
}
range.ColumnWidth = 15;
Clipboard.SetImage(image);
excelWorksheet.Paste(range, false);
}
}
else
{
// Column does not contain an image
excelWorksheet.Cells[j + 2, k + 1] = dataGridViewAcoes.Rows[j].Cells[k].Value.ToString();
}
}
}


SaveFileDialog exportExcelFile = new SaveFileDialog();
exportExcelFile.FileName = "Ações";
exportExcelFile.DefaultExt = "xlsx";

if (exportExcelFile.ShowDialog() == DialogResult.OK)
{
excelWorkbook.SaveAs(exportExcelFile.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
excelWorksheet.Columns.AutoFit();
}
catch (Exception)
{
throw;
}
}
private void button1_Click(object sender, EventArgs e)
{
_Application excelApp;
_Workbook excelWorkbook;
_Worksheet excelWorksheet;

try
{
excelApp = new Microsoft.Office.Interop.Excel.Application();
excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
excelWorksheet = null;

excelWorksheet = excelWorkbook.Sheets["Folha1"];
excelWorksheet = excelWorkbook.ActiveSheet;
excelWorksheet.Name = "Dados exportados";

for (int j = 0; j < dataGridViewAcoes.Rows.Count; j++)
{
for (int k = 0; k < dataGridViewAcoes.Columns.Count; k++)
{
if (dataGridViewAcoes.Columns[k] is DataGridViewImageColumn)
{
// Column contains an image
byte[] imageData = (byte[])dataGridViewAcoes.Rows[j].Cells[k].Value;
if (imageData != null)
{
// Convert byte array to image
MemoryStream ms = new MemoryStream(imageData);
Image image = Image.FromStream(ms);

// Insert image into cell
Excel.Range range = (Excel.Range)excelWorksheet.Cells[j + 2, k + 1];
range.RowHeight = image.Height;
if (range.RowHeight < 0 || range.RowHeight > 409)
{
// If row height is outside acceptable range, set to default value
range.RowHeight = excelWorksheet.DefaultRowHeight;
}
range.ColumnWidth = 15;
Clipboard.SetImage(image);
excelWorksheet.Paste(range, false);
}
}
else
{
// Column does not contain an image
excelWorksheet.Cells[j + 2, k + 1] = dataGridViewAcoes.Rows[j].Cells[k].Value.ToString();
}
}
}


SaveFileDialog exportExcelFile = new SaveFileDialog();
exportExcelFile.FileName = "Ações";
exportExcelFile.DefaultExt = "xlsx";

if (exportExcelFile.ShowDialog() == DialogResult.OK)
{
excelWorkbook.SaveAs(exportExcelFile.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
excelWorksheet.Columns.AutoFit();
}
catch (Exception)
{
throw;
}
}
And this is the error that's giving: Severity Code Description Project Project Classification File Line Suppression Status Error CS1061 '_Worksheet' does not contain a definition for "DefaultRowHeight" and no extension method "DefaultRowHeight" could be found that accepts a first argument of type '_Worksheet' (are you forgetting to use a directive or an assembly reference? ) PontosAbertos 1 C:\Users\infprog\Desktop\PA\PontosAbertos\Acoes.cs 299 Active But i am using: using Excel = Microsoft.Office.Interop.Excel;
2 replies
CC#
Created by yumo on 2/27/2023 in #help
❔ date difference
Quick question, why is this not giving me any result on my column resultado?
horaInicio is nvarchar(max) horaFim nvarchar(max) resultado nvarchar(max)
// get the value of the "horaInicio" column
DateTime horaInicio = (DateTime)row.Cells["horaInicio"].Value;

// get the value of the "horaFim" column
DateTime horaFim = (DateTime)row.Cells["horaFim"].Value;

// calculate the duration between the two times
TimeSpan duration = horaFim - horaInicio;

// set the value of the "resultado" column to the duration in hours
row.Cells["resultado"].Value = duration.TotalHours;
// get the value of the "horaInicio" column
DateTime horaInicio = (DateTime)row.Cells["horaInicio"].Value;

// get the value of the "horaFim" column
DateTime horaFim = (DateTime)row.Cells["horaFim"].Value;

// calculate the duration between the two times
TimeSpan duration = horaFim - horaInicio;

// set the value of the "resultado" column to the duration in hours
row.Cells["resultado"].Value = duration.TotalHours;
2 replies
CC#
Created by yumo on 2/23/2023 in #help
❔ login system saves data from user
Hi this is the code that i have and i dont know why but its showing me the username and not the realname on the label, why? Button on Form1 to check if user credentials are correct and if user exists open MainMenu and display the realname of the user logged in.
private void btnEntrar_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=(localdb)\\MSSqlLocalDB;Initial Catalog=oil;Integrated Security=True;Pooling=False";
string username = txtUsername.Text;
string password = txtPassword.Text;
UserData loggedInUser = null;

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = new SqlCommand("SELECT * FROM Users WHERE username = @username AND password = @password", connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);

SqlDataReader reader = command.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int id = reader.GetInt32(0);
string realname = reader.GetString(1);
string nivel = reader.GetString(2);

// create a new User object with the retrieved data
loggedInUser = new UserData(id, realname, nivel);

// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}

reader.Close();
}
}
private void btnEntrar_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=(localdb)\\MSSqlLocalDB;Initial Catalog=oil;Integrated Security=True;Pooling=False";
string username = txtUsername.Text;
string password = txtPassword.Text;
UserData loggedInUser = null;

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = new SqlCommand("SELECT * FROM Users WHERE username = @username AND password = @password", connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);

SqlDataReader reader = command.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int id = reader.GetInt32(0);
string realname = reader.GetString(1);
string nivel = reader.GetString(2);

// create a new User object with the retrieved data
loggedInUser = new UserData(id, realname, nivel);

// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}

reader.Close();
}
}
15 replies
CC#
Created by yumo on 2/22/2023 in #help
❔ sql statment Count *
Why is this not displaying on my textbox txtEnsaio the max number of Ensaios that are in the table tbl_ensaios with the id that's on the txtIdFerramenta?
private void funcGetEnsaioNumber()
{
string str = "SELECT COUNT(*) FROM tbl_ensaios WHERE idProjetos = @idProjetos";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@idProjetos", txtIdFerramenta.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtEnsaioID.Text = dr[0].ToString();
}
con.Close();
}
private void funcGetEnsaioNumber()
{
string str = "SELECT COUNT(*) FROM tbl_ensaios WHERE idProjetos = @idProjetos";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@idProjetos", txtIdFerramenta.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtEnsaioID.Text = dr[0].ToString();
}
con.Close();
}
i want to get the id from the txtIdFerramenta and get the max number of rows with that same id on the tbl_ensaios. This is just giving me 0. I run this sql statment on a query on the database and gets me the result
6 replies
CC#
Created by yumo on 2/16/2023 in #help
❔ SELECT MAX ID from textbox
447 replies
CC#
Created by yumo on 2/14/2023 in #help
❔ Combobox add more text into data
4 replies
CC#
Created by yumo on 2/10/2023 in #help
❔ Help in sql client query Update
35 replies
CC#
Created by yumo on 1/20/2023 in #help
❔ Login Sql error
25 replies
CC#
Created by yumo on 1/20/2023 in #help
Error with app send email
39 replies
CC#
Created by yumo on 1/12/2023 in #help
❔ Textbox multi data
Im trying to use my barcode reader and its going alright, but i want to make a textbox that with the data that is filled with , it would insert into my mysqlce database in different columns, like these RH each space it gives to go into another row it would create a new row https://gyazo.com/6703c423c7a125f4882a0d408aa2cc80
5 replies
CC#
Created by yumo on 1/11/2023 in #help
❔ EVENT ON TEXT CHANGED
private void txtRH_TextChanged(object sender, EventArgs e)
{

}
private void txtRH_TextChanged(object sender, EventArgs e)
{

}
How can i make a button disable after the textbox is field?
6 replies
CC#
Created by yumo on 1/6/2023 in #help
✅ crud error checkbox not working
5 replies