绑定国家和州时面临错误 [英] Facing error while binding country and state

查看:113
本文介绍了绑定国家和州时面临错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是asp.net的新手我做了一个注册表单,我需要在我的应用程序中绑定国家和州,即当我选择国家时我需要显示相关的状态。我有详细信息它在SQL服务器中。

还需要选择dob并自动获取年龄请求帮助。谢谢提前



< b>我尝试了什么:



Hi I am new to asp.net I made a Registration form and i need to bind Country and state in my application ie When I select the country I need the state related to be displayed.I have the details of it in SQL server.
Also Need to select the dob and to get the age automatically pleas help me.Thanks in Advance

What I have tried:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;


namespace Test
{
    public partial class RegistrationForm : System.Web.UI.Page
    {
        //string CS = ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString;
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString);


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAll();
            }
        }

        public void BindAll()
        {
            cn.Open();
            SqlCommand cmd = new SqlCommand("select CountryName,CounID from Country_details", cn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            DDlCountry.DataSource = dt;
            DDlCountry.DataTextField = "CountryName";
            DDlCountry.DataValueField = "CounID";
            DDlCountry.DataBind();
            cn.Close();
        }
        public void Bind_ddlState()
        {
            cn.Open();

            SqlCommand cmd = new SqlCommand("select StateName,CounID from State_details where CounID='" + DDlCountry.SelectedValue + "'", cn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            Ddlstate.DataSource = dt;
            Ddlstate.DataTextField = "StateName";
            Ddlstate.DataValueField = "CounID";
            Ddlstate.DataBind();
            Ddlstate.Enabled = true;
            cn.Close();
        }




        protected void txtDOB_SelectionChanged(object sender, EventArgs e)
        {

        }




        protected void Button1_Click1(object sender, EventArgs e)
        {
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO RegistrationDetails1 (Name, Department,Salary,Doj,Dob, Age,Country,State,Phone,Email) VALUES ('" + txtname.Text + "','" + txtDept.Text + "','" + txtSalary.Text + "','" + txtDOJ.Text + "','" + txtDOB.Text + "','" + txtAge.Text + "','" + DDlCountry.Text + "','" + Ddlstate.Text + "','" + txtPhone.Text + "','" + TxtEmail.Text + "','" + txtPin.Text + "' )", cn);
                cmd.ExecuteNonQuery();
                cn.Close();
            }

        }


        protected void Button2_Click1(object sender, EventArgs e)
        {
            this.ClearCachedClientID();

        }



        protected void cal()
        {

            if (Calendar2.Visible == false)
            {
                Calendar2.Visible = true;

            }
            else
            {
                Calendar2.Visible = false;
            }
            if (Calendar1.Visible == false)
            {
                Calendar1.Visible = true;
            }
            else
            {
                Calendar1.Visible = false;
            }
        }

        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            txtDOB.Text = Convert.ToString(Calendar1.SelectedDate);
        }

        protected void txtDOB_TextChanged(object sender, EventArgs e)
        {

        }

        protected void DDlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddltest_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind_ddlState();
        }




    }
}

推荐答案

一个大问题是你将值直接连接到SQL语句。这使您对SQL注入和转换问题等持开放态度。正确的方法是使用 SqlParameter类(System.Data.SqlClient) [ ^ ]



另一件事是你不喜欢处置对象,以便不正确释放资源。此外,您应该添加适当的错误处理来处理常见的异常。



例如,请查看正确执行数据库操作 [ ^ ]



日历的内容,看起来你正在从Calendar1获取值Calendar2_SelectionChanged。这是故意吗?
One big problem is that you concatenate directly the values to SQL statement. This leaves you wide open to SQL injection and conversion problems etc. The correct way is to use SqlParameter Class (System.Data.SqlClient)[^]

Another thing is that you don't dispose the objects so resources are not correctly freed. Also you should add proper error handling to handle common exceptions.

For examples have a look at Properly executing database operations[^]

What comes to the calendar, it looks like you're taking the value from Calendar1 in Calendar2_SelectionChanged. Is this intentional?


Quote:

国家/地区绑定正确,但状态不具约束力我选择一个特定的国家。

The country is binding properly,but the state is not binding when i select a particular country.



你需要注意几件事,一个是 AutoPostBack 对于DDlCountry来说应该是真的下面你需要正确地给出选定的索引更改事件名称


there are few things you need to note, one is AutoPostBack should be true for the DDlCountry as below and you need to give the selected index change event name correctly

<asp:DropDownList id="DDlCountry" runat="server" AutoPostBack="true" OnSelectIndexChanged="DDlCountry_SelectedIndexChanged"/>



现在更改 DDlCountry_SelectedIndexChanged ,如下所示


now change the DDlCountry_SelectedIndexChanged as below

protected void DDlCountry_SelectedIndexChanged(object sender, EventArgs e)
      {
        Bind_ddlState();
      }


1。从DDlCountry_SelectedIndexChanged(..)事件调用Bind_ddlState()但从不同事件调用方法。

2.从设计视图或属性窗口为DDLCountry设置AutoPostback =true。
1. Call Bind_ddlState() from DDlCountry_SelectedIndexChanged (..) event but method was called from different event.
2. Set AutoPostback="true" for DDLCountry from design view or else from properties window.


这篇关于绑定国家和州时面临错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆