Hi! I used sql and Asp.Net, but couldn’t get all data from sql and show it’s in GridView. Because some column is null. I want show all data not null and null in GridView.
CREATE TABLE [dbo].[Information](
[Id] [int] IDENTITY(1,1) NOT NULL,
[LastName] [nchar](25) NULL,
[Name] [nchar](25) NULL,
[FatherName] [nchar](25) NULL,
[Birthday] [date] NULL,
[Address] [nchar](40) NULL,
[Param] [int] NULL )
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Form.aspx.cs" Inherits="WebProb.Form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button" runat="server" onclick="Button_Click" Text="List" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace WebProb
{
public partial class Form : System.Web.UI.Page
{
string UserData = System.Configuration.ConfigurationManager.AppSettings.Get("Base");//connection
protected void Page_Load(object sender, EventArgs e)
{
}
public List<object> GetData(int kod)
{
SqlConnection con = new SqlConnection(UserData);
SqlCommand cmd = con.CreateCommand();
List<object> s = new List<object>();
string sql = "select Id,LastName,Name,FatherName,Birthday,Address from information where param ='" + kod.ToString() + "'";
cmd.CommandText = sql;
con.Open();
try
{
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
s.Add(new
{
Id = rdr.GetInt32(0),
LastName = rdr.GetString(1),
Name = rdr.GetString(2),
FatherName = rdr.GetString(3),
Birthday = rdr.GetDateTime(4),
Address = rdr.GetString(5),
});
}
}
catch { }
con.Close();
return s;
}
protected void Button_Click(object sender, EventArgs e)
{
GridView1.DataSource = GetData(1).ToArray();
GridView1.DataBind();
}
}
}