我该如何解决这个请帮助我。 [英] How Do I Solve This Please Help Me.
问题描述
Error:System.NullReferenceException: Object reference not set to an instance of an object. at Samregistration.Button1_Click(Object sender, EventArgs e)<br />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Configuration;
using System.Data;
public partial class Samregistration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ConnectionString);
conn.Open();
string insertQuery ="insert into Regstration2 (UserName,E-mail,Password,Country) values (@Uname,@email,@password,@country)";
SqlCommand com = new SqlCommand (insertQuery, conn);
com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
com.Parameters.AddWithValue("@email",TextBoxEM.Text);
com.Parameters.AddWithValue("@password",TextBoxp.Text);
com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());
com.ExecuteNonQuery();
Response.Write("Registration is sucessfull");
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error:"+ex.ToString());
}
}
}
推荐答案
看起来你正在尝试使用整个连接字符串作为连接字符串的键。当您在ConfigurationManager.ConnectionStrings
上调用索引时,我希望您应该传递一个名称/密钥来标识配置中的连接字符串,但是您正在通过整个连接字符串作为名称/键。
尝试更改
It looks like you're trying to use the entire connection string as a key to a connection string. When you call the indexed onConfigurationManager.ConnectionStrings
I would expect that you're supposed to pass a name/key that identifies a connection string in the config, but you're passing the entire connection string as name/key.
Try changing
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ConnectionString);
to
to
SqlConnection conn= new SqlConnection ("Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"]);
希望这会有所帮助,
Fredrik
Hope this helps,
Fredrik
最佳猜测 - 组合框中没有选定的项目,因此它在此行上失败:
Best guess - no selected item in the combo box, so it fails on this line:
com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());
检查方法顶部的值,确保在尝试转到数据库之前所有用户输入都有效。例如:输入的用户名是?它有独特之处吗?是否有电子邮件,是否为有效格式?
此外:
1)您的连接字符串名称不应相同作为连接字符串:
Check your values at the top of the method and make sure that all the user inputs are valid before you even try to go to the database. For example: is the username entered? Is it unique? Is there an email, and is it a valid format?
In addition:
1) Your connection string name should not be the same as the connection string:
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ConnectionString);
Shoudl有点像:
Shoudl be somethign like:
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Registration"].ConnectionString);
。
2)您应该处理Connection和Command对象 - 如果出现问题,您的代码会保持连接处于打开状态,因为您没有将其关闭 catch
块。考虑使用使用
块来确保在完成对象时始终销毁对象:
.
2) You should Dispose of Connection and Command objects - your code leaves the connection open if there is a problem because you don't close it in the catch
block. Consider using a using
block to ensure objects are always destroyed when you are finished with them:
try
{
using (SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Registration"].ConnectionString))
{
conn.Open();
string insertQuery ="insert into Regstration2 (UserName,E-mail,Password,Country) values (@Uname,@email,@password,@country)";
using (SqlCommand com = new SqlCommand (insertQuery, conn))
{
com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
com.Parameters.AddWithValue("@email",TextBoxEM.Text);
com.Parameters.AddWithValue("@password",TextBoxp.Text);
com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());
com.ExecuteNonQuery();
Response.Write("Registration is sucessfull");
}
}
}
catch (Exception ex)
{
Response.Write("Error:"+ex.ToString());
}
3)请不要以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ]
另见:代码犯罪:基于文本的密码 [ ^ ]
这篇关于我该如何解决这个请帮助我。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!