Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing an accounting appliction and i create database and forms
i want to check if ther is a data at table "TBcompany " after log in the appliction :

1-if there is a data at the table "TBcompany " : get filed value to text box.
2-if there is no data at the table"TBcompany " open form "company" to record data

What I have tried:

i used select * count to check if the table HasRow
but i dont know how to check the table if there is no data at the table to open the form company to record the data.
Posted
Updated 9-Sep-22 6:07am
v3

SQL
SELECT COUNT(*) FROM MyTable
will return the number of rows: if it's zero (and you can use SqLConnection.ExecuteScalar to get just that result) there is no data in the table.

But ... I'm not sure you understand this stuff: a database table will normally have more than one row, and a textbox is pretty much a "single item" display element; it isn't good at showing multiple rows.

So I think you want to go back a step and work out exactly what you are trying to achieve here: normally a DataGridView or similar is a better display choice for a table full of data!
 
Share this answer
 
Comments
mohamed thrwat 8-Sep-22 4:21am    
i have Main Form " FRM_MAIN" Showup after succesful login From "FRM_LOGIN", This Main Form Have a Textboxs Shows Ex: Textbox "Logername" which i get it from Login user After Login, i have another Textbox i want to show company name in it , so if this is the first time to use then there is no Data At Company Table That What i Want To Do Is To Open Frm_Company as There is No Data To Record New Data To Get The Company Name Show in Textbox .
sorry if my english is bad to deliver the idea
OriginalGriff 8-Sep-22 6:09am    
And?
What part of that is giving you a problem?

I'm not trying to be annoying - I just can't see your screen!
mohamed thrwat 9-Sep-22 12:05pm    
i have solved it . thanks for your help
OriginalGriff 9-Sep-22 12:47pm    
You're welcome!
C#
private void FRM_MAIN2_Load(object sender, EventArgs e)
        {
            label14.Text = FRM_LOGIN.loginuserid;
            label4.Text = FRM_LOGIN.loginuser;

            var DAL = new SQlcls.DataAccessLayer();
            var dt = new DataTable();
            var Cmd = new SqlCommand();
            dt.Clear();
            Cmd = new SqlCommand("SELECT id, COUNT(*) FROM company  GROUP BY id Having COUNT(*) > 0", DAL.SQLCON);
            if (DAL.SQLCON.State == ConnectionState.Open)
                DAL.SQLCON.Close();
                DAL.SQLCON.Open();
            SqlDataReader dr = Cmd.ExecuteReader();
            dr.Read();
            if (dr.HasRows)
               
            {
                label15.Text= dr[0].ToString();
            }
           
            else

            {
                MessageBox.Show("يجب اضافة شركة اولا ", "تأكيد", MessageBoxButtons.OK);
                FRM_COMPANY form = new FRM_COMPANY();
                form.Show();

                //searchcomp();
                //searchcompname();
                //fyear();
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900