One Error Left: Severity Code Description Project File Line Suppression State Er
ID: 3859086 • Letter: O
Question
One Error Left:
Severity
Code
Description
Project
File
Line
Suppression State
Error
CS1729
'SqlDataReader' does not contain a constructor that takes 0 arguments
AddressBook
34
Active
Default.aspx
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AddressBook
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void rep_bind()
{
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"); //here you need mention your Datasource details
string query = "select * from customer where Name like'" + TextBox1.Text + "%'";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader dr = new SqlDataReader();
if (dr.HasRows)
{
dr.Read();
rep_bind();
GridView1.Visible = true;
TextBox1.Text = "";
Label1.Text = "";
}
else
{
GridView1.Visible = false;
Label1.Visible = true;
Label1.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Record";
}
}
}
}
Label.cs
namespace AddressBook
{
internal class Label1
{
internal static bool Visible;
internal static string Text;
}
}
Severity
Code
Description
Project
File
Line
Suppression State
Error
CS1729
'SqlDataReader' does not contain a constructor that takes 0 arguments
AddressBook
34
Active
Explanation / Answer
I read the error and the program both.
The error is on this line: SqlDataReader dr = new SqlDataReader();
There's no constructor like SqlDataReader() which is a 0 argument constructor. This is not the way to get the object of SqlDataReader.
To obtain an object of it, you should write like:
SqlCommand command = new SqlCommand(<your sql query like select *>, <sql connection object);
SqlDataReader reader = command.executeReader();
At first you need to create an SqlCommand object which contains the SQL query on the SqlConnection.
And on that SqlCommand you can get the SqlDataReader object.
So you should write something as per this on your button click event.
So as per your application, there should be first the query and then the SqlDataReader because you are directly using dr.HasRows but you are not mentioning the query. If there's any query, do comment, I'll help in it.
Do comment if there is any query. Thank you. :)
Edit:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
SqlCommand command = new SqlCommand("select * from customer where Name like'" + TextBox1.Text + "%'", con);
SqlDataReader dr = command.executeReader();
if (dr.HasRows)
{
dr.Read();
rep_bind();
GridView1.Visible = true;
TextBox1.Text = "";
Label1.Text = "";
}
else
{
GridView1.Visible = false;
Label1.Visible = true;
Label1.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Record";
}
}
This is the way to proceed with SqlDataReader. At first you are defining command which is a sql query on your database connection object and then you are executing the query using SqlDataReader. As this is the select statement, it will read some information from the file so executeReader() is run.
So, now you will have the dr object containing the rows as per the select statement.
Check this and comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.