I have created a function loaddata()
to get the records from my database and to display it in my gridview. When I execute it, the id
column value is not getting displayed.
private void loadData()
{
var userdata = db.getUsers();
userIDGV.DataPropertyName = "ID";
userGV.DataPropertyName = "User";
usernameGV.DataPropertyName = "Username";
passwordGV.DataPropertyName = "Password";
phoneGV.DataPropertyName = "Phone";
addressGV.DataPropertyName = "Address";
roleNameGV.DataPropertyName = "Role";
dataGridView1.DataSource = userdata;
MainClass.sno(dataGridView1, "snoGV");
}
This is the query of my stored procedure getusers()
:
ALTER PROCEDURE [dbo].[getUsers]
AS
SELECT
u.u_id AS 'ID',
u.u_name AS 'User',
u.u_uname AS 'Username',
u.u_password AS 'Password',
u.u_phone AS 'Phone',
u.u_address AS 'Address',
u.u_rollID AS 'RoleID',
r.r_name AS 'Role'
FROM
users u
INNER JOIN
roles r on r.r_id = u.u_rollID
ORDER BY
u.u_id ASC
The value is getting stored in database but it is not getting displayed in the datagridview when I run the loadData()
. What is the issue? Please help me out.
I am also new to C# but I always use the below method to display the data from my database in a gridview.
public void disp_data()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Your Query here";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
Write your query instead of "Your Query here" and change the dataGridView1 to your desired dataFridView. Now call this disp_data() function in your form_load.