ASP.NET - Error :

Asked By vimalkumar panneerselvam on 29-Oct-10 11:32 AM

CS1502: The best overloaded method match for 'System.Data.SqlClient.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlConnection)' has some invalid arguments

Can anyone help me....


My code :

void
BindGrid()

{


Dataset ds = new Dataset();

SqlCommand cmd = new SqlCommand();

SqlConnection conn = new SqlConnection ();

DataGrid grdsrch = new DataGrid();

try

{

cmd.CommandText = "MKDB_connstring";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@mode", "SELECT"));

cmd.Parameters.Add(new SqlParameter("@searchfield", DropDownList1.SelectedValue.ToString));  //ERROR showing here

cmd.Parameters.Add(new SqlParameter("@searchvalue", TextBox1.Text.ToString)); //ERROR showing here

SqlDataAdapter adp = new SqlDataAdapter();

cmd.Connection = con;

con.open();           //ERROR showing here

adp = new SqlDataAdapter(cmd);

adp.Fill(ds);             //ERROR showing here

DataGrid1.DataSource = ds;        //ERROR showing here

DataGrid1.DataBind();          //ERROR showing here

con.Close();

}

catch (Exception ex)

{


Response.Write("Error :" + ex.Message.ToString() + "< br />" + ex.StackTrace);

}

}

This looks wrong

Robbe Morris replied to vimalkumar panneerselvam on 29-Oct-10 12:00 PM

cmd.CommandText = "MKDB_connstring";



.CommandText should contain the name of your stored procedure.  This looks like a connection string variable name.  I also do not see where you set the con.ConnectionString property. 
anil soni replied to vimalkumar panneerselvam on 29-Oct-10 12:16 PM
I believe that error is not in the given lines of code and is somewhere else. I will ask you to provide mre detail like line number if you are sure that the error is in same block of code.

You can ceck rest of your code where ever you are creating a new instance of SqlCommand object. You are givning incorrect parameters there. Make sure whereever you have created an instance of SqlCommand with two arguments in its contrutor the first one is stirng and the second one is a instance of SqlConnection class.

Hopefully it will resolve your eerror....
vimalkumar panneerselvam replied to Robbe Morris on 29-Oct-10 12:16 PM
Thanks for your information..
but still there is a problem in these two lines....

cmd.Parameters.Add(new SqlParameter("@searchfield", ddSearch.SelectedValue.ToString));

cmd.Parameters.Add(new SqlParameter("@searchvalue", txtSearch.Text.ToString));

How is the code you posted compiling?
Robbe Morris replied to vimalkumar panneerselvam on 29-Oct-10 12:23 PM

cmd.Connection = con;

con.open();      

Your variable above for SqlConnection is conn not con.  Visual Studio should have prevented you from compiling this code and running it.

vimalkumar panneerselvam replied to Robbe Morris on 29-Oct-10 12:28 PM
Sorry I hav renamed tat but still keep sayng the error...Am tryng to solve for half a day....Can u help me plz



using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI.WebControls;

namespace WebApplication2

{


public partial class Search : System.Web.UI.Page

{


protected void Page_Load(object sender, EventArgs e)

{


}//end page load

protected void Button1_Click(object sender, EventArgs e) //Search Button

{

if (DropDownList1 != null && TextBox1.Text != "" )

{

TextBox1.Text = TextBox1.Text.Replace("'", "''");

BindGrid();

TextBox1.Text = TextBox1.Text.Replace("''", "'");

}

else


lblMessage.Text = "No Values Provided to search !";

lblMessage.ForeColor = System.Drawing.Color.Red;

}


void BindGrid()

{

DataSet ds = new DataSet();

SqlCommand cmd = new SqlCommand();

SqlConnection con = new SqlConnection();

DataGrid DataGrid1 = new DataGrid();

DropDownList ddSearch = new DropDownList();

SqlDataAdapter adp = new SqlDataAdapter();

TextBox txtSearch = new TextBox();

try

{

cmd.CommandText = "sp_search";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@mode", "SELECT"));

cmd.Parameters.Add(new SqlParameter("@searchfield", ddSearch.SelectedValue.ToString));

cmd.Parameters.Add(new SqlParameter("@searchvalue", txtSearch.Text.ToString));

cmd.Connection = con;

con.Open();

adp = new SqlDataAdapter(cmd); //mention the command

adp.Fill(ds);

DataGrid1.DataSource = ds;

DataGrid1.DataBind();

}

catch (Exception ex)

{

Response.Write("Error :" + ex.Message.ToString() + "< br />" + ex.StackTrace);

}

finally

{

con.Close();

}

}



protected void Button2_Click(object sender, EventArgs e)

{

Response.Redirect("/Default.aspx", true);

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

}

protected void TextBox1_TextChanged(object sender, EventArgs e)

{

}

protected void Gridsearch_SelectedIndexChanged(object sender, EventArgs e)

{

}


public SqlConnection con { get; set; }

}

}

undhad ashwin replied to vimalkumar panneerselvam on 29-Oct-10 10:07 PM
Can u replace u r below code

cmd.Parameters.Add(new SqlParameter("@searchfield", DropDownList1.SelectedValue.ToString()));  //ERROR showing here

cmd.Parameters.Add(new SqlParameter("@searchvalue", TextBox1.Text.ToString())); //ERROR

if any query then replay