Member 13366591 Ответов: 2

Ошибка определения местоположения указанного сервера/экземпляра?


Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)


Что я уже пробовал:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace OnlineExam
{
    public partial class Registration : System.Web.UI.Page
    {
        SqlConnection SqlCon;
       SqlDataAdapter SqlAdp;
        //SqlConnection SqlAdp;
        DataSet ds;
        string strFN = "", strLN = "", strD = "", strG = "", strY = "", strM = "", strUN = "", strP = "", strCP = "", strCN = "", strE = "", strCat = "", strCountry = "", StrState = "", strCity = "";
        string str12 = "";
        int x;
        protected void Page_Load(object sender, EventArgs e)
        {

            //str12 = Request.QueryString.Get("str");
            // Response.Write(str12);
            txtP.Attributes.Add("value", txtP.Text);
            txtCp.Attributes.Add("value", txtCp.Text);
            Connection co = new Connection();
            string strcon = co.ConnectionString();

            SqlCon = new SqlConnection(strcon);

            if (Page.IsPostBack == false)
            {
                showrecords();
            }
            lblAvail.Text = "";

        }

        void showrecords()
        {

            SqlAdp = new SqlDataAdapter("select * from tb_country ", SqlCon);
            ds = new DataSet();
            SqlAdp.Fill(ds);
            ddcountry.DataSource = ds;
            ddcountry.DataBind();
            ds.Dispose();
        }


        protected void ddcountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlAdp = new SqlDataAdapter("select * from tb_State where country_id=" + ddcountry.SelectedValue, SqlCon);
            ds = new DataSet();
            SqlAdp.Fill(ds);
            ddState.DataSource = ds;
            ddState.DataBind();
            ds.Dispose();

        }
        protected void ddState_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlAdp = new SqlDataAdapter("select * from tb_city where state_id='" + ddState.SelectedValue + "'", SqlCon);
            ds = new DataSet();
            SqlAdp.Fill(ds);
            ddCity.DataSource = ds;
            ddCity.DataBind();
            ds.Dispose();
        }

        protected void ddM_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddY.SelectedValue == "Year")
            {
                lblmess.Text = "Please Select Year First";
            }
            else
            {
                if (ddY.SelectedValue == "Year")
                {
                    lblmess.Text = "Please Select Year First";
                }
                else
                {
                    //lblmess.Visible = false;
                    int y = Convert.ToInt32(ddY.SelectedItem.Text);

                    int i = ddM.SelectedIndex;

                    switch (i)
                    {
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            ddD.Items.Remove("31");
                            ddD.Items.Remove("30");
                            ddD.Items.Remove("29");
                            ddD.Items.Insert(29, "29");
                            ddD.Items.Insert(30, "30");
                            ddD.Items.Insert(31, "31");
                            break;
                        case 2:
                            if ((y % 4 == 0) || (y % 10 == 0 && y % 4 == 0))
                            {
                                ddD.Items.Remove("31");
                                ddD.Items.Remove("30");

                            }
                            else
                            {

                                ddD.Items.Remove("31");
                                ddD.Items.Remove("30");
                                ddD.Items.Remove("29");
                            }
                            break;

                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            ddD.Items.Remove("31");
                            ddD.Items.Remove("30");
                            ddD.Items.Remove("29");
                            ddD.Items.Insert(29, "29");
                            ddD.Items.Insert(30, "30");
                            break;

                    }
                }

            }
        }

        protected void ddY_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblmess.Text = "";
            int y = Convert.ToInt32(ddY.SelectedItem.Text);
            int i = ddM.SelectedIndex;

            if (((y % 4 == 0) || (y % 10 == 0 && y % 4 == 0)) && i == 2)
            {
                ddD.Items.Remove("31");
                ddD.Items.Remove("30");
                ddD.Items.Remove("29");
                ddD.Items.Insert(29, "29");

            }
            else if (i == 2)
            {
                ddD.Items.Remove("31");
                ddD.Items.Remove("30");
                ddD.Items.Remove("29");
            }


        }



        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            //string str12 = "";
            //str12 = Request.QueryString.Get("str");
            string str12 = Convert.ToString(rbtn_stu_tea.SelectedValue);
            if (str12 == "Upload the test")
            {
                SqlAdp = new SqlDataAdapter("select count(*) from tb_teacher_reg where UserName='" + txtUserN.Text + "'", SqlCon);
            }
            else
            {
                SqlAdp = new SqlDataAdapter("select count(*) from tb_student where UserName='" + txtUserN.Text + "'", SqlCon);
            }
            if (SqlCon.State == ConnectionState.Closed)
                SqlCon.Open();
            int x = Convert.ToInt16(SqlAdp.SelectCommand.ExecuteScalar());

            if (x == 0)
            {

                lblAvail.Text = "Valid User Name";
                strFN = txtFn.Text;
                strLN = txtL.Text;
                strP = txtP.Text;
                strCP = txtCp.Text;
                strUN = txtUserN.Text;
                strE = txtEmail.Text;
                strG = ddgen.SelectedItem.Text;
                strCountry = ddcountry.SelectedItem.Text;
                strCity = ddCity.SelectedItem.Text;
                StrState = ddState.SelectedItem.Text;
                //strCat = ddCategry.SelectedItem.Text;
                strCN = txtCoNo.Text;
                strY = ddY.SelectedItem.Text;
                strM = ddM.SelectedItem.Text;
                strD = ddD.SelectedItem.Text;



                if (str12 == "Upload The Test")
                {
                    SqlAdp = new SqlDataAdapter("Insert into tb_teacher_reg(Fname,Lname,Date,Gender,UserName,Password,ContactNo,Emailid,Country,State,City) values('" + strFN + "','" + strLN + "',('" + strD + " " + strM + "," + strY + "'),'" + strG + "','" + strUN + "','" + strP + "','" + strCN + "','" + strE + "','" + strCountry + "','" + StrState + "','" + strCity + "')", SqlCon);
                }

                else
                {
                    SqlAdp = new SqlDataAdapter("Insert into tb_student(Fname,Lname,Date,Gender,UserName,Password,ContactNo,Emailid,Country,State,City) values('" + strFN + "','" + strLN + "',('" + strD + " " + strM + "," + strY + "'),'" + strG + "','" + strUN + "','" + strP + "','" + strCN + "','" + strE + "','" + strCountry + "','" + StrState + "','" + strCity + "')", SqlCon);
                }
                ds = new DataSet();
                SqlAdp.Fill(ds);
                ds.Dispose();

                // Response.Write("<script>alert('hi')</script>");
                Response.Redirect("Login.aspx?str=" + rbtn_stu_tea.SelectedValue);
            }

            else
                lblAvail.Text = "User Name Already Exist";
        }





        protected void btnAvail_Click1(object sender, EventArgs e)
        {
            lblAvail.Visible = true;
            SqlAdp = new SqlDataAdapter("select count(*) from tb_student where UserName='" + txtUserN.Text + "'", SqlCon);
            if (SqlCon.State == ConnectionState.Closed)
                SqlCon.Open();
            int x = Convert.ToInt16(SqlAdp.SelectCommand.ExecuteScalar());

            if (x == 0)
                lblAvail.Text = "Valid User Name";

            else
                lblAvail.Text = "User Name Already Exist";
        }
    }
}

2 Ответов

Рейтинг:
1

Graeme_Grant

У вас проблема со строкой подключения. Прочтите о OriginalGriff[^] ответ на аналогичный вопрос несколько часов назад: Имя Sql server". " не работает[^]


Рейтинг:
1

Dave Kreskowiak

Google для "атаки SQL-инъекций", чтобы узнать, почему то, что вы делаете для создания строки SQL-запроса, так плохо.

Затем погуглите "параметризованные запросы C# SQL", чтобы узнать, что с этим делать.