I’m trying to use SQL Functions with SQLDataAdabter, but when i run my application i get this exception :
Object reference not set to an instance of an object
in that line :
adbtr.SelectCommand.CommandType = CommandType.Text;
And when i remove that line, i get the same exception but in the line after:
adbtr.SelectCommand.CommandText = "SELECT * FROM Select_gallery_names_FN()";
Here’s my code :
protected void Page_Load(object sender, EventArgs e)
{
DataSet dst = new DataSet();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
{
SqlDataAdapter adbtr = new SqlDataAdapter();
adbtr.SelectCommand.CommandType = CommandType.Text;
adbtr.SelectCommand.CommandText = "SELECT * FROM Select_gallery_names_FN()";
try
{
int result = adbtr.Fill(dst);
if (result == 0)
{
return;
}
cat_repeater.DataSource = dst;
cat_repeater.DataBind();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
Any help would be appreciated. Thanks.
You need to initialize the
SelectCommand.. it isnullat the time you’re setting properties on it: