初始化字符串的格式不符合从索引0开始的规范。 [英] Format of the initialization string does not conform to specification starting at index 0.

查看:83
本文介绍了初始化字符串的格式不符合从索引0开始的规范。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击注册按钮时我得到了异常



I get thie exception when i click the register button

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Data;
using System.Text.RegularExpressions;
namespace Sup
{
    /// <summary>
    /// Interaction logic for Signup.xaml
    /// </summary>
    public partial class Signup : Window
    {
        public Signup()
        {
            InitializeComponent();
        }
        private void Reset()
        {
            textBoxAddress.Text = "";
            textBoxEmail.Text = "";
            textBoxFirstName.Text = "";
            textBoxLastName.Text = "";
            passwordBox1.Password = "";
            passwordBoxConfirm.Password = "";
        }
        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxEmail.Text.Length == 0)
            {

                MessageBox.Show("Enter an email.");

                textBoxEmail.Focus();

            }

            else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
            {

                MessageBox.Show("Enter a valid email.");

                textBoxEmail.Select(0, textBoxEmail.Text.Length);

                textBoxEmail.Focus();

            }

            else
            {

                string firstname = textBoxFirstName.Text;

                string lastname = textBoxLastName.Text;

                string email = textBoxEmail.Text;

                string password = passwordBox1.Password;

                if (passwordBox1.Password.Length == 0)
                {

                    MessageBox.Show("Enter password.");

                    passwordBox1.Focus();

                }

                else if (passwordBoxConfirm.Password.Length == 0)
                {

                    MessageBox.Show("Enter Confirm password.");

                    passwordBoxConfirm.Focus();

                }

                else if (passwordBox1.Password != passwordBoxConfirm.Password)
                {

                    MessageBox.Show("Confirm password must be same as password.");

                    passwordBoxConfirm.Focus();

                }

                else
                {

                    string address = textBoxAddress.Text;

                    SqlConnection con = new SqlConnection("Properties.Settings.Default.usersConnectionString");
                    
                        con.Open();

                        SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con);

                        cmd.CommandType = CommandType.Text;

                        cmd.ExecuteNonQuery();

                        con.Close();

                        MessageBox.Show("You have Registered successfully.");

                        Reset();
                    
                }

            }
        }
    }
}

推荐答案

))
{

MessageBox.Show( 输入有效的电子邮件。);

textBoxEmail.Select( 0 ,textBoxEmail.Text。长度);

textBoxEmail.Focus();

}

其他
{

string firstname = textBoxFirstName.Text;

string lastname = textBoxLastName.Text;

string email = textBoxEmail.Text;

< span class =code-keyword> string password = passwordBox1.Password;

if (passwordBox1.Password.Length == 0
{

MessageBox.Show( 输入密码。);

passwordBox1.Focus();

}

else if (passwordBoxConfirm。 Password.Length == 0
{

MessageBox.Show( 输入确认密码。);

passwordBoxConfirm.Focus();

}

else if (passwordBox1。密码!= passwordBoxConfirm.Password)
{

MessageBox.Show( 确认密码必须与密码相同。);

passwordBoxConfirm.Focus();

}

else
{

string address = textBoxAddress.Text;

SqlConnection con = new SqlConnection( Properties.Settings.Default.usersConnectionString);

con.Open();

SqlCommand cmd = new SqlCommand( 插入注册(FirstName,LastName,电子邮件,密码,地址)值(' + firstname + ',' + lastname + ',' + email + ',' +密码+ ',' +地址+ ') ,con);

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show( 您已成功注册。);

重置();

}

}
}
}
}
")) { MessageBox.Show("Enter a valid email."); textBoxEmail.Select(0, textBoxEmail.Text.Length); textBoxEmail.Focus(); } else { string firstname = textBoxFirstName.Text; string lastname = textBoxLastName.Text; string email = textBoxEmail.Text; string password = passwordBox1.Password; if (passwordBox1.Password.Length == 0) { MessageBox.Show("Enter password."); passwordBox1.Focus(); } else if (passwordBoxConfirm.Password.Length == 0) { MessageBox.Show("Enter Confirm password."); passwordBoxConfirm.Focus(); } else if (passwordBox1.Password != passwordBoxConfirm.Password) { MessageBox.Show("Confirm password must be same as password."); passwordBoxConfirm.Focus(); } else { string address = textBoxAddress.Text; SqlConnection con = new SqlConnection("Properties.Settings.Default.usersConnectionString"); con.Open(); SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("You have Registered successfully."); Reset(); } } } } }


首先检查你的输入字符串:这是非常非常错误的!

Start by checking your input string: It's very, very wrong!
SqlConnection con = new SqlConnection("Properties.Settings.Default.usersConnectionString");



您正在传递一个固定的字符串作为连接字符串,而您可能希望传递设置信息的内容。



尝试删除双引号:


You are passing a fixed string as the connection string, when probably you want to pass the content of the setting information instead.

Try removing the double quotes:

SqlConnection con = new SqlConnection(Properties.Settings.Default.usersConnectionString);

我不保证会修复它 - 取决于什么你有在设置文件中 - 但它确实有更好的机会!

I don't guarantee that will fix it - depends on what you have in the settings file - but it does stand a much better chance!


在这一行



On this line

SqlConnection con = new SqlConnection("Properties.Settings.Default.usersConnectionString");





删除引号。您传递字符串Properties.Settings.Default.usersConnectionString而不是该属性的值。


这篇关于初始化字符串的格式不符合从索引0开始的规范。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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