Quantcast
Channel: CSharp Forum Latest Questions
Viewing all articles
Browse latest Browse all 32059

Object reference not set to an instance of an object

$
0
0
Hello Frnds,

    I am working on a hospital project. when i want to register patient details with my webform m getting Object reference not set to an instance of an object  .here is code.

please check let me know what i have to do for resolve this error.

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

public partial class ipregister : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection("server=PC\\SQLEXPRESS;user id=sa;password=password@123;database=Hospitalmanagement");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            filldrop();
           DropDownList5.Items.Insert(0, "...Select...");
        }
        string dt = DateTime.Now.ToShortDateString();
        string dtm = DateTime.Now.ToShortTimeString();
        TextBox9.Text = dt;
        TextBox10.Text = dtm;
        cn.Open();
        int pid =0;
         int b = 0;

Here is Error in this code


        pid  = Convert.ToInt32(new SqlCommand("select patientid from hospital_inpatient order by 1 desc", cn).ExecuteScalar().ToString()) + 1;
        b = Convert.ToInt32(new SqlCommand("select admid from hospital_inpatient order by 1 desc", cn).ExecuteScalar().ToString()) + 1;
         

TextBox2.Text =  pid.ToString();
         TextBox8.Text = b.ToString();
        cn.Close();


    }
    void filldrop()
    {
        cn.Open();
        string qry = "select name from hospital_doctorsignup";
        SqlDataAdapter da = new SqlDataAdapter(qry, cn);
        DataSet ds = new DataSet();
        da.Fill(ds, "hospital_doctorsignup");
        DropDownList5.DataSource = ds;
        DropDownList5.DataTextField = "name";
        DropDownList5.DataBind();
        cn.Close();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_hospital_inpatient";
        cmd.Connection = cn;

        SqlParameter p = new SqlParameter("@patientname",SqlDbType.VarChar,20);
        p.Value = TextBox1.Text;
        cmd.Parameters.Add(p);


        SqlParameter p1 = new SqlParameter("@gender", SqlDbType.VarChar, 20);
        p1.Value = DropDownList1.Text;
        cmd.Parameters.Add(p1);

        SqlParameter p2 = new SqlParameter("@age", SqlDbType.Int);
        p2.Value = TextBox3.Text;
        cmd.Parameters.Add(p2);

        SqlParameter p3 = new SqlParameter("@address", SqlDbType.VarChar, 20);
        p3.Value = TextBox4.Text;
        cmd.Parameters.Add(p3);

        SqlParameter p4 = new SqlParameter("@phoneres", SqlDbType.BigInt);
        p4.Value = TextBox5.Text;
        cmd.Parameters.Add(p4);

        SqlParameter p5 = new SqlParameter("@phonemob", SqlDbType.BigInt);
        p5.Value = TextBox6.Text;
        cmd.Parameters.Add(p5);

        SqlParameter p6 = new SqlParameter("@maritual", SqlDbType.VarChar, 20);
        p6.Value = DropDownList2.Text;
        cmd.Parameters.Add(p6);

        SqlParameter p7 = new SqlParameter("@occupation", SqlDbType.VarChar, 20);
        p7.Value = TextBox7.Text;
        cmd.Parameters.Add(p7);

        SqlParameter p8 = new SqlParameter("@admid", SqlDbType.Int);
        p8.Value = TextBox8.Text;
        cmd.Parameters.Add(p8);

        SqlParameter p9 = new SqlParameter("@admdate", SqlDbType.DateTime);
        p9.Value = TextBox9.Text;
        cmd.Parameters.Add(p9);

        SqlParameter p10 = new SqlParameter("@admtime", SqlDbType.DateTime);
        p10.Value = TextBox10.Text;
        cmd.Parameters.Add(p10);

        SqlParameter p11 = new SqlParameter("@status", SqlDbType.VarChar, 20);
        p11.Value = DropDownList4.Text;
        cmd.Parameters.Add(p11);

        SqlParameter p12 = new SqlParameter("@symtoms", SqlDbType.VarChar, 20);
        p12.Value = TextBox12.Text;
        cmd.Parameters.Add(p12);

        SqlParameter p13 = new SqlParameter("@department", SqlDbType.VarChar, 20);
        p13.Value = DropDownList3.Text;
        cmd.Parameters.Add(p13);

        SqlParameter p14 = new SqlParameter("@wardno", SqlDbType.Int);
        p14.Value = TextBox14.Text;
        cmd.Parameters.Add(p14);

        SqlParameter p15 = new SqlParameter("@bedno", SqlDbType.Int);
        p15.Value = TextBox15.Text;
        cmd.Parameters.Add(p15);

        SqlParameter p16 = new SqlParameter("@doctor", SqlDbType.VarChar,20);
        p16.Value = DropDownList5.Text;
        cmd.Parameters.Add(p16);

        cmd.ExecuteNonQuery();
        cn.Close();
        Response.Redirect("doctorsden.aspx");

    }
}




Aspex Code

<%@ Page Language="C#" Debug ="true"  MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ipregister.aspx.cs" Inherits="ipregister" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table style="z-index: 102; left: 168px; font-family: 'Lucida Console'; position: absolute;
        top: 232px">
        <tr>
            <td style="width: 135px">
                <span style="color: #ffffff; font-family: Lucida Console">Patientname:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
            <td style="width: 100px">
                <span style="color: #ffffff; font-family: Lucida Console">AdmissionID:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox8" runat="server" Enabled="false"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 135px">
                <span style="font-size: 11pt; color: #ffffff; font-family: Lucida Console">Patient ID:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox2" runat="server" Enabled="false"></asp:TextBox></td>
            <td style="width: 100px">
                <span style="color: #ffffff; font-family: Lucida Console">AdmissionDate:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox9" runat="server" ></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 135px">
                <span style="color: #ffffff; font-family: Lucida Console">Gender:</span></td>
            <td style="width: 100px">
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>...select...</asp:ListItem>
                    <asp:ListItem>Male</asp:ListItem>
                    <asp:ListItem>Female</asp:ListItem>
                </asp:DropDownList></td>
            <td style="width: 100px">
                <span style="color: #ffffff; font-family: Lucida Console">AdmissionTime:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox10" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 135px; height: 26px">
                <span style="color: #ffffff; font-family: Lucida Console">Age:</span></td>
            <td style="width: 100px; height: 26px">
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
            <td style="width: 100px; height: 26px">
                <span style="color: #ffffff; font-family: Lucida Console">Status:</span></td>
            <td style="width: 100px; height: 26px"><asp:DropDownList ID="DropDownList4" runat="server" Width="96px">
                <asp:ListItem>...select...</asp:ListItem>
                <asp:ListItem>Starting</asp:ListItem>
                <asp:ListItem>Mild</asp:ListItem>
                <asp:ListItem>Serious</asp:ListItem>
            </asp:DropDownList></td>
        </tr>
        <tr>
            <td style="width: 135px">
                <span style="color: #ffffff; font-family: Lucida Console">Address:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox4" runat="server" TextMode="MultiLine"></asp:TextBox></td>
            <td style="width: 100px">
                <span style="color: #ffffff; font-family: Lucida Console">Symptoms:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 135px">
                <span style="color: #ffffff; font-family: Lucida Console">Phoneno(resi):</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></td>
            <td style="width: 100px">
                <span style="color: #ffffff">Department:</span></td>
            <td style="width: 100px"><asp:DropDownList ID="DropDownList3" runat="server">
                <asp:ListItem>...select...</asp:ListItem>
                    <asp:ListItem>Cardiology</asp:ListItem>
                    <asp:ListItem>Orthopedic</asp:ListItem>
                    <asp:ListItem>Gynacology</asp:ListItem>
                    <asp:ListItem>Dental</asp:ListItem>
                    <asp:ListItem>Opthology</asp:ListItem>
                </asp:DropDownList></td>
        </tr>
        <tr>
            <td style="width: 135px">
                <span style="color: #ffffff; font-family: Lucida Console">Phoneno(mob):</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></td>
            <td style="width: 100px">
                <span style="color: #ffffff">AssignedDoctor:</span></td>
            <td style="width: 100px">
                <asp:DropDownList ID="DropDownList5" runat="server" Width="96px">
                    <asp:ListItem>...select...</asp:ListItem>
                </asp:DropDownList></td>
        </tr>
        <tr>
            <td style="width: 135px">
                <span style="color: #ffffff; font-family: Lucida Console">Martialstatus:</span></td>
            <td style="width: 100px">
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem>...select...</asp:ListItem>
                    <asp:ListItem>Married</asp:ListItem>
                    <asp:ListItem>Unmarried</asp:ListItem>
                </asp:DropDownList></td>
            <td style="width: 100px">
                <span style="color: #ffffff">Wardno:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox14" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 135px">
                <span style="color: #ffffff; font-family: Lucida Console">Occupation:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox></td>
            <td style="width: 100px">
                <span style="color: #ffffff">Bedno:</span></td>
            <td style="width: 100px">
                <asp:TextBox ID="TextBox15" runat="server"></asp:TextBox></td>
        </tr>
    </table>
    <asp:Label ID="Label1" runat="server" Font-Names="Modern" Font-Size="24pt" ForeColor="Red"
        Style="z-index: 100; left: 344px; position: absolute; top: 181px" 
        Text="In-Patient Registration"></asp:Label>
    <table style="z-index: 103; left: 376px; position: absolute; top: 512px">
        <tr>
            <td style="width: 100px">
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 100;
                    left: 0px; position: absolute; top: 0px" Text="Submit" />
            </td>
            <td style="width: 100px">
                <asp:Button ID="Button2" runat="server" Text="Reset" /></td>
        </tr>
    </table>
</asp:Content>


Viewing all articles
Browse latest Browse all 32059

Trending Articles