错误.你调用的对象是空的

use*_*620 0 c# object

以前一切都很好.我唯一做的就是添加了一个编辑按钮,然后发生了这个错误.即使我撤消编辑按钮,错误仍然存​​在.谁能告诉我第26行有什么问题?

SqlCommand cmdDoctorInfo = new SqlCommand("SELECT d.* FROM Doctorinfo p, Department d WHERE d.DepartmentID = p.DepartmentID AND p.userId = @UserID", conDoctorInfo);
Line 26:   cmdDoctorInfo.Parameters.AddWithValue("@UserID", Session["UserId"].ToString()); 
SqlDataReader dtrPatient = cmdDoctorInfo.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)

编辑:由于一些不明原因,我关闭并一遍又一遍地打开应用程序后突然正常工作.现在,当我添加回编辑按钮时,错误会恢复.这是编辑按钮代码(第2行)

<td align="center">
                    <asp:Button ID="editButton" runat="server" OnClick="editButton_Click" Text="Edit" />
                    <asp:FileUpload ID="picFileUpload" runat="server"/>
                    <asp:Button ID="uploadButton" runat="server" OnClick="uploadButton_Click" Text="Upload"/>
                    <br />
                    <asp:Label ID="messageLabel" runat="server" ForeColor="Red"></asp:Label>
                </td>
Run Code Online (Sandbox Code Playgroud)

我不明白添加"编辑"按钮会导致错误.请赐教.

Muh*_*air 6

可能是你的Session ["UserId"]为空.试试这个:

cmdDoctorInfo.Parameters.AddWithValue("@UserID",Convert.ToString(Session["UserId"]));
Run Code Online (Sandbox Code Playgroud)

即使Session ["UserId"]为null,Convert.ToString也不会破坏您的代码.因为Convert.ToString()处理null,而ToString()不处理null.