Vishwanath Magge
Vishwanath Magge

Reputation: 11

Cannot implicitly convert type 'WebApplication4.SqlConnection' to 'System.Data.SqlClient.SqlConnection' ~AddUser.aspx.cs

I have tried so many types I don't know how to insert.

Is there any problem with sql server?

PLEASE HELP ME FAST..

SqlConnection con = new SqlConnection( "Data Source=localhost/SQLEXPRESS.Polaris.dbo;Initial Catalog=Polaris;Integrated Security=True;Pooling=False");

protected void Page_Load(object sender, EventArgs e)
{
        con.Open();
}

protected void Button3_Click(object sender, EventArgs e)
{
    con.Open();

    SqlDataReader rdr = null;
    //string s1 = "insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'";
    SqlCommand cmd1 = new SqlCommand("insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'");
    cmd1.Connection = con;
    rdr = cmd1.ExecuteReader();
    Label2.Visible = true;
    //EndEventHandler.RemoveAll();
 }

 protected void Button2_Click(object sender, EventArgs e)
 {
        Response.Redirect("WebForm1.aspx");
 }

Upvotes: 0

Views: 1662

Answers (2)

Damith
Damith

Reputation: 63095

Cannot implicitly convert type 'WebApplication4.SqlConnection' to 'System.Data.SqlClient.SqlConnection'

According to error you have class called SqlConnection in WebApplication4 namespace. You may have mistakenly generate that class. you need to remove that class first and then add reference to System.Data.SqlClient

Upvotes: 1

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

Here's what you can do:

SqlConnection con = new SqlConnection( "Data Source=localhost/SQLEXPRESS.Polaris.dbo;Initial Catalog=Polaris;Integrated Security=True;Pooling=False");
protected void Button3_Click(object sender, EventArgs e)
{
    con.Open();
    string s1 = "insert into Login values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "'";
    SqlCommand cmd = new SqlCommand(s1, con);
    cmd.ExecuteNonQuery();
}

Upvotes: 0

Related Questions