How to fix “'ServerVersion' threw an exception of type 'System.InvalidOperationException'”? [duplicate]

邮差的信 提交于 2019-12-05 14:23:40

Step 1: go to WebConfig File and Write this Code:

enter code here

 <connectionStrings>

 <add name ="MyDbConn" ---> write same it is 

     connectionString="Server=SYED\SQLEXPRESS; database=Templete_2_DB; 
     Trusted_Connection=True" providerName="System.data.sqlclient"
     />
  </connectionStrings>

code here

SYED\SQLEXPRESS; ---> this is your servername Templete_2_DB; ----> this is your database name

step 2: Go to your page event and write code like this..

enter code here


 SqlConnection con = new SqlConnection(

 WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);

 SqlCommand cmd = new SqlCommand("select * from Accounts_Data where 
 UserName=@username and Password=@password", con);
        cmd.Parameters.AddWithValue("@username", txt_username.Text);
        cmd.Parameters.AddWithValue("@password", txt_userPassword.Text);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();

        if (dt.Rows.Count > 0)
        {
            Response.Redirect("Default.aspx");
        }

code here

Go ahead... in this you will be guided

https://www.youtube.com/watch?v=Mo0ECWKVVDU

Sheebu Ansari

Create a method:

private void connection()
{
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStrSheebu"].ConnectionString);
}

set in web.config as:

<connectionStrings>
    <add name="conStrSheebu" connectionString="Data Source=(local);Initial Catalog=Sheebu;User ID=sa;Password = AnsarI" providerName="System.Data.SqlClient" />
</connectionStrings>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!